JSON Array: Difference between revisions
m (Conversion script moved page JSON Array to jSON Array: Converting page titles to lowercase) |
No edit summary |
||
(2 intermediate revisions by the same user not shown) | |||
Line 9: | Line 9: | ||
An array of strings. Note that each name is quoted and the whole array has single quotes around it. | An array of strings. Note that each name is quoted and the whole array has single quotes around it. | ||
< | <syntaxhighlight lang="mtmacro"> | ||
[h: beatles = '["Paul", "John", "George", "Ringo"]'] | [h: beatles = '["Paul", "John", "George", "Ringo"]'] | ||
</ | </syntaxhighlight> | ||
An array of prime numbers. The numbers should not be quoted but the whole array has quotes around it. | An array of prime numbers. The numbers should not be quoted but the whole array has quotes around it. | ||
< | <syntaxhighlight lang="mtmacro"> | ||
[h: primes = "[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]"] | [h: primes = "[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]"] | ||
</ | </syntaxhighlight> | ||
A better way to initialize an array as you'll be less likely to have issues with quoting. | A better way to initialize an array as you'll be less likely to have issues with quoting. | ||
< | <syntaxhighlight lang="mtmacro"> | ||
[h: list = json.append("[]", "Peter", "Paul", "Mary")] | [h: list = json.append("[]", "Peter", "Paul", "Mary")] | ||
</ | </syntaxhighlight> | ||
Access the elements of a JSON array with {{func|json.get}} or other JSON Functions. | Access the elements of a JSON array with {{func|json.get}} or other JSON Functions. | ||
< | <syntaxhighlight lang="mtmacro"> | ||
[h: list = json.append("[]", "Peter", "Paul", "Mary")] | [h: list = json.append("[]", "Peter", "Paul", "Mary")] | ||
[r: json.get(list,2)] | [r: json.get(list,2)] | ||
</ | </syntaxhighlight> | ||
Returns: Mary | Returns: Mary | ||
[[Category:Variable Type]] | [[Category:Variable Type]] |
Latest revision as of 23:59, 15 March 2023
The JSON Array is a native JSON Data Type and supported by many MapTool macro functions as an input or output.
JSON.org defines it like this:
An array is an ordered collection of values. An array begins with
[
(left bracket) and ends with]
(right bracket). Values are separated by a,
(comma).
An array may hold any of the supported JSON Data Types: Number, String, Object, Array, Boolean and Null.
Examples of JSON Arrays
An array of strings. Note that each name is quoted and the whole array has single quotes around it.
[h: beatles = '["Paul", "John", "George", "Ringo"]']
An array of prime numbers. The numbers should not be quoted but the whole array has quotes around it.
[h: primes = "[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]"]
A better way to initialize an array as you'll be less likely to have issues with quoting.
[h: list = json.append("[]", "Peter", "Paul", "Mary")]
Access the elements of a JSON array with json.get() or other JSON Functions.
[h: list = json.append("[]", "Peter", "Paul", "Mary")]
[r: json.get(list,2)]
Returns: Mary