JSON Array: Difference between revisions

From RPTools Wiki
Jump to navigation Jump to search
m (Taustin moved page jSON Array to JSON Array over redirect)
No edit summary
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.
<source lang="mtmacro">
<syntaxhighlight lang="mtmacro">
[h: beatles = '["Paul", "John", "George", "Ringo"]']
[h: beatles = '["Paul", "John", "George", "Ringo"]']
</source>
</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.
<source lang="mtmacro">
<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]"]
</source>
</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.
<source lang="mtmacro">
<syntaxhighlight lang="mtmacro">
[h: list = json.append("[]", "Peter", "Paul", "Mary")]
[h: list = json.append("[]", "Peter", "Paul", "Mary")]
</source>
</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.
<source lang="mtmacro">
<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)]
</source>
</syntaxhighlight>
Returns: Mary
Returns: Mary


[[Category:Variable Type]]
[[Category:Variable Type]]

Revision as of 18:41, 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