JSON Array: Difference between revisions

From RPTools Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 20: Line 20:
[h: list = json.append("[]", "Peter", "Paul", "Mary")]
[h: list = json.append("[]", "Peter", "Paul", "Mary")]
</source>
</source>
Access the elements of a JSON array with {{func|json.get}} or other JSON Functions.
<source lang="mtmacro">
[h: list = json.append("[]", "Peter", "Paul", "Mary")]
[r: json.get(list,2)]
</source>
Returns: Paul


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

Revision as of 17:40, 6 July 2020

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: Paul