JSON Object: Difference between revisions

From RPTools Wiki
Jump to navigation Jump to search
m (Typographical tweaks)
m (Added Languages tag)
 
(7 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{stub}}
{{Languages|JSON Object}}
An object is an '''unordered''' set of name/value pairs. An object begins with {{code|{}} (left brace) and ends with {{code|<nowiki>}</nowiki>}} (right brace). Each name is followed by {{code|:}} (colon) and the name/value pairs are separated by {{code|,}} (comma).<br>
The JSON Object is a native JSON Data Type and supported by many MapTool macro functions as an input or output.
- [http://www.json.org JSON.org]
 
[http://www.json.org JSON.org] defines it like this:
<blockquote>An object is an '''unordered''' set of name/value pairs. An object begins with {{code|{}} (left brace) and ends with {{code|<nowiki>}</nowiki>}} (right brace). Each name is followed by {{code|:}} (colon) and the name/value pairs are separated by {{code|,}} (comma).
</blockquote>
 
The values in an object's name/value pairs may be of any of the supported JSON Data Types: Number, String, Object, Array, Boolean and Null. The names must be strings.
 
== Examples of JSON Objects ==
A simple object with two name/value pairs.
<syntaxhighlight lang="mtmacro">
[h: flower = '{"type":"Rose","color":"Red"}']
</syntaxhighlight>
The same object created with {{func|json.set}}.
<syntaxhighlight lang="mtmacro">
[h: flower = json.set("","type","Rose","color","Red")]
</syntaxhighlight>
A more complex example with an object in an object.
<syntaxhighlight lang="mtmacro">
[h: flower = json.set("","type","Rose","color","Red")]
[h: flower = json.set(flower,"quantity",12)]
[h: order =  json.set("","customer","Hopeless Romantic","address","End of Lonely Street", "flower",flower)]
<pre>[r: json.indent(order,2)]</pre>
</syntaxhighlight>
Produces:
<syntaxhighlight lang="javascript">
{
  "customer": "Hopeless Romantic",
  "address": "End of Lonely Street",
  "flower":  {
    "type": "Rose",
    "color": "Red",
    "quantity": 12
  }
}
</syntaxhighlight>
 
-
[[Category:Variable Type]]
[[Category:Variable Type]]
{{Languages|JSON Object}}

Latest revision as of 05:11, 20 September 2024

Languages:  English  • 日本語

The JSON Object 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 object is an unordered set of name/value pairs. An object begins with { (left brace) and ends with } (right brace). Each name is followed by : (colon) and the name/value pairs are separated by , (comma).

The values in an object's name/value pairs may be of any of the supported JSON Data Types: Number, String, Object, Array, Boolean and Null. The names must be strings.

Examples of JSON Objects

A simple object with two name/value pairs.

[h: flower = '{"type":"Rose","color":"Red"}']

The same object created with json.set().

[h: flower = json.set("","type","Rose","color","Red")]

A more complex example with an object in an object.

[h: flower = json.set("","type","Rose","color","Red")]
[h: flower = json.set(flower,"quantity",12)]
[h: order =  json.set("","customer","Hopeless Romantic","address","End of Lonely Street", "flower",flower)]
<pre>[r: json.indent(order,2)]</pre>

Produces:

{
  "customer": "Hopeless Romantic",
  "address": "End of Lonely Street",
  "flower":   {
    "type": "Rose",
    "color": "Red",
    "quantity": 12
  }
}

-

Languages:  English  • 日本語