json.fields: Difference between revisions

From RPTools Wiki
Jump to navigation Jump to search
m (Added line explaining that JSON Objects are unordered)
(Reordering and rewriting toward a more standard presentation)
Line 4: Line 4:
|description=
|description=
Returns the fields (keys) for a [[JSON Object]] or the indexes for a [[JSON Array]] as a string list. The type of the value returned depends on the delimiter parameter.  
Returns the fields (keys) for a [[JSON Object]] or the indexes for a [[JSON Array]] as a string list. The type of the value returned depends on the delimiter parameter.  
* If the delimiter is not specified then a [[Macros:string list|string list]] is returned with the default value of ',' is used.
* If the delimiter ''"json"'' then a [[JSON Array]] is returned.
* Otherwise a [[Macros:string list|string list]] is returned with the delimiter passed in.


Note: Because [[JSON Object]]s are unordered, ''json.fields'' will not reflect the order they were set in.
<u>Note</u>: Because [[JSON Object]]s are unordered, ''json.fields'' will not reflect the order they were set in.


|usage=
|usage=
<source lang="mtmacro" line>
<source lang="mtmacro" line>
[h: strList = json.fields(jobj)]
json.fields(jobj)
[h: strList = json.fields(jobj, delim)]
</source>
[h: jobj = json.fields(jobj, 'json')]
<source lang="mtmacro" line>
json.fields(jobj, delim)
</source>
</source>
'''Parameters'''
* {{code|delim}} =
** {{code|unspecified}}: a standard [[Macros:string list|string list]] is returned, with its default {{code|","}} delimiter.
** {{code|"json"}}: a [[JSON Array]] is returned.
** {{code|"x"}}: a [[Macros:string list|string list]] is returned with {{code|"x"}} used as a delimiter.


|example=
|example=
<source lang="mtmacro" line>
<source lang="mtmacro" line>
  [h:a=json.fromStrProp("a=1;b=44;c=12")] [r:json.fields(a)]
[h: a=json.fromStrProp("a=1;b=44;c=12")]
   [h:a=json.fromList("a,1,g,4")][r:json.fields(a)]
[r: json.fields(a)]
  [h:a=json.fromStrProp("a=1;b=44;c=12")] [r:json.fields(a, "json")]
</source>
Returns
   b,c,a
 
<source lang="mtmacro" line>
[h: a=json.fromList("a,1,g,4")]
[r: json.fields(a,";")]
</source>
</source>
Returns
  0;1;2;3


<source lang="mtmacro" line>
[h: a=json.fromStrProp("a=1;b=44;c=12")]
[r: json.fields(a, "json")]
</source>
Returns
Returns
  a,c,b
  0,1,2,3
   ["a","c","b"]
   ["a","c","b"]
}}
}}
[[Category:JSON Function]]
[[Category:JSON Function]]

Revision as of 13:00, 10 February 2012

json.fields() Function

Introduced in version 1.3b49
Returns the fields (keys) for a JSON Object or the indexes for a JSON Array as a string list. The type of the value returned depends on the delimiter parameter. Note: Because JSON Objects are unordered, json.fields will not reflect the order they were set in.

Usage

json.fields(jobj)
json.fields(jobj, delim)

Parameters

  • delim =
    • unspecified: a standard string list is returned, with its default "," delimiter.
    • "json": a JSON Array is returned.
    • "x": a string list is returned with "x" used as a delimiter.

Example

[h: a=json.fromStrProp("a=1;b=44;c=12")]
[r: json.fields(a)]

Returns

 b,c,a
[h: a=json.fromList("a,1,g,4")]
[r: json.fields(a,";")]

Returns

 0;1;2;3
[h: a=json.fromStrProp("a=1;b=44;c=12")]
[r: json.fields(a, "json")]

Returns

["a","c","b"]