JSON Array: Difference between revisions

From RPTools Wiki
Jump to navigation Jump to search
m (Typographical tweaks)
No edit summary
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 array is an '''ordered''' collection of values. An array begins with {{code|[}} (left bracket) and ends with {{code|]}} (right bracket). Values are separated by {{code|,}} (comma).<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.
<source lang="mtmacro">
[h: beatles = '["Paul", "John", "George", "Ringo"]']
</source>
An array of prime numbers.  The numbers should not be quoted but the whole array has quotes around it.
<source lang="mtmacro">
[h: primes = "[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]"]
</source>
A better way to initialize an array as you'll be less likely to have issues with quoting.
<source lang="mtmacro">
[h: list = json.append("[]", "Peter", "Paul", "Mary")]
</source>
 
[[Category:Variable Type]]
[[Category:Variable Type]]

Revision as of 14:09, 6 October 2019

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")]