json.indexOf: Difference between revisions
Jump to navigation
Jump to search
mNo edit summary |
No edit summary |
||
(3 intermediate revisions by 2 users not shown) | |||
Line 6: | Line 6: | ||
|usage= | |usage= | ||
< | <syntaxhighlight lang="mtmacro" line> | ||
json.indexOf(array, value) | json.indexOf(array, value) | ||
</ | </syntaxhighlight> | ||
< | <syntaxhighlight lang="mtmacro" line> | ||
json.indexOf(array, value, start) | json.indexOf(array, value, start) | ||
</ | </syntaxhighlight> | ||
'''Parameters''' | '''Parameters''' | ||
{{param|array|The [[JSON Array]] to search.}} | {{param|array|The [[JSON Array]] to search.}} | ||
Line 19: | Line 19: | ||
|examples= | |examples= | ||
Find the index of the first occurrence of {{code|1}}: | Find the index of the first occurrence of {{code|1}}: | ||
< | <syntaxhighlight lang="mtmacro" line> | ||
[r: json.indexOf("[1,2,3,1,1,3]", 1)] | [r: json.indexOf("[1,2,3,1,1,3]", 1)] | ||
</ | </syntaxhighlight> | ||
Returns: {{code|0}} | Returns: {{code|0}} | ||
Find the index of the first occurrence of {{code|1}}, starting at index {{code|1}}: | Find the index of the first occurrence of {{code|1}}, starting at index {{code|1}}: | ||
< | <syntaxhighlight lang="mtmacro" line> | ||
[r: json.indexOf("[1,2,3,1,1,3]", 1, 1)] | [r: json.indexOf("[1,2,3,1,1,3]", 1, 1)] | ||
</ | </syntaxhighlight> | ||
Returns: {{code|3}} | Returns: {{code|3}} | ||
Find the index of the first occurrence of {{code|2}}, starting at index {{code|2}}: | Find the index of the first occurrence of {{code|2}}, starting at index {{code|2}}: | ||
< | <syntaxhighlight lang="mtmacro" line> | ||
[r: json.indexOf("[1,2,3,1,1,3]", 2, 2)] | [r: json.indexOf("[1,2,3,1,1,3]", 2, 2)] | ||
</ | </syntaxhighlight> | ||
Returns: {{code|-1}} | Returns: {{code|-1}} | ||
Line 39: | Line 39: | ||
In this example, [[json.path.read]] gets the object with the unique key {{code|currency}} set to {{code|Gold}} in an array of JSON objects representing money. Note that [[json.get]] is used because json.path.read always returns the object in an array.<br> | In this example, [[json.path.read]] gets the object with the unique key {{code|currency}} set to {{code|Gold}} in an array of JSON objects representing money. Note that [[json.get]] is used because json.path.read always returns the object in an array.<br> | ||
The {{code|currency}} key is presupposed to be unique for every record in this example: | The {{code|currency}} key is presupposed to be unique for every record in this example: | ||
< | <syntaxhighlight lang="mtmacro" line> | ||
[h: moneyJSON = '[ | [h: moneyJSON = '[ | ||
{"currency":"Copper", "amount":9}, | {"currency":"Copper", "amount":9}, | ||
Line 47: | Line 47: | ||
[h: justGold = json.get(json.path.read(moneyJSON, "[?(@.currency == 'Gold')]"), 0)] | [h: justGold = json.get(json.path.read(moneyJSON, "[?(@.currency == 'Gold')]"), 0)] | ||
[r: json.indexOf(moneyJSON, justGold)] | [r: json.indexOf(moneyJSON, justGold)] | ||
</ | </syntaxhighlight> | ||
Returns: {{code|2}} | Returns: {{code|2}} | ||
Latest revision as of 23:59, 15 March 2023
json.indexOf() Function
• Introduced in version 1.3b53
Returns the index of the first occurrence of a value in the JSON Array. If the value does not exist in the JSON Array then
-1
is returned. All JSON Array indexes start at 0
.Usage
json.indexOf(array, value)
json.indexOf(array, value, start)
Parameters
array
- The JSON Array to search.value
- The value to find the index of in the JSON Array.start
- The index to start searching from, if not specified it defaults to0
.
Examples
Find the index of the first occurrence of
Returns:
1
:
[r: json.indexOf("[1,2,3,1,1,3]", 1)]
Returns: 0
Find the index of the first occurrence of 1
, starting at index 1
:
[r: json.indexOf("[1,2,3,1,1,3]", 1, 1)]
Returns: 3
Find the index of the first occurrence of 2
, starting at index 2
:
[r: json.indexOf("[1,2,3,1,1,3]", 2, 2)]
Returns: -1
Finding JSON Objects in an Array. You can also find the indexes of a JSON object in a JSON array.
In this example, json.path.read gets the object with the unique key currency
set to Gold
in an array of JSON objects representing money. Note that json.get is used because json.path.read always returns the object in an array.
The currency
key is presupposed to be unique for every record in this example:
[h: moneyJSON = '[
{"currency":"Copper", "amount":9},
{"currency":"Silver", "amount":14},
{"currency":"Gold", "amount":5}
]']
[h: justGold = json.get(json.path.read(moneyJSON, "[?(@.currency == 'Gold')]"), 0)]
[r: json.indexOf(moneyJSON, justGold)]
2