JSON Array: Difference between revisions

From RPTools Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
(8 intermediate revisions by 6 users not shown)
Line 1: Line 1:
{{stub}}
The JSON Array is a native JSON Data Type and supported by many MapTool macro functions as an input or output.
"An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence."<br>
 
- from [http://www.json.org JSON.org]
[http://www.json.org JSON.org] defines it like this:
<blockquote>An array is an '''ordered''' collection of values. An array begins with {{code|[}} (left bracket) and ends with {{code|]}} (right bracket). Values are separated by a {{code|,}} (comma).</blockquote>
 
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.
<syntaxhighlight lang="mtmacro">
[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.
<syntaxhighlight lang="mtmacro">
[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.
<syntaxhighlight lang="mtmacro">
[h: list = json.append("[]", "Peter", "Paul", "Mary")]
</syntaxhighlight>
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")]
[r: json.get(list,2)]
</syntaxhighlight>
Returns: Mary
 
[[Category:Variable Type]]
[[Category:Variable Type]]

Latest 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