json.contains: Difference between revisions

From RPTools Wiki
Jump to navigation Jump to search
(New page: ==Function json.contains== '''Introduced in MapTool 1.3b49''' Returns 1 if the json object contains the specified key. ===Usage=== <source lang="mtmacro" line> ...)
 
No edit summary
 
(5 intermediate revisions by 2 users not shown)
Line 1: Line 1:
==Function json.contains==
{{MacroFunction
|name=json.contains
|version=1.3b49
|description=
Returns {{true}} if the [[JSON Object]] contains the specified key, or if the [[JSON Array]] contains the specified value.


|usage=
<syntaxhighlight lang="mtmacro" line>
json.contains(object, key)
</syntaxhighlight>
<syntaxhighlight lang="mtmacro" line>
json.contains(array, value)
</syntaxhighlight>
'''Parameters'''
{{param|object|The [[JSON Object]] to test for the key.}}
{{param|key|The key to check the object for.}}
{{param|array|The [[JSON Array]] to test for the value.}}
{{param|value|The value to check the array for.}}


|examples=
Check if the [[JSON Object]] {{code|a}} contains the key {{code|"b"}}:
<syntaxhighlight lang="mtmacro" line>
[h:a=json.fromStrProp("a=1;b=44;c=12")]
[json.contains(a,"b")]
</syntaxhighlight>
Returns {{true}}


'''Introduced in MapTool 1.3b49'''
Check if the [[JSON Object]] {{code|a}} contains the key {{code|"z"}}:
<syntaxhighlight lang="mtmacro" line>
[h:a=json.fromStrProp("a=1;b=44;c=12")]
[json.contains(a,"z")]
</syntaxhighlight>
Returns {{false}}


Returns 1 if the [[Macros:json object|json object]] contains the specified key.
Check if the [[JSON Array]] {{code|a}} contains the value {{code|"b"}}:
<syntaxhighlight lang="mtmacro" line>
[h:a=json.fromList("1,b,3,d")]
[json.contains(a,"b")]
</syntaxhighlight>
Returns {{true}}


===Usage===
|also=
<source lang="mtmacro" line>
{{func|json.type}}
[h: value = json.contains(jobj, key)]
</source>


|changes=
{{change|1.3b51|Added ability for function to work with [[JSON Array]]s.}}


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

Latest revision as of 18:44, 15 March 2023

json.contains() Function

Introduced in version 1.3b49
Returns true(1) if the JSON Object contains the specified key, or if the JSON Array contains the specified value.

Usage

json.contains(object, key)
json.contains(array, value)

Parameters

  • object - The JSON Object to test for the key.
  • key - The key to check the object for.
  • array - The JSON Array to test for the value.
  • value - The value to check the array for.

Examples

Check if the JSON Object a contains the key "b":
[h:a=json.fromStrProp("a=1;b=44;c=12")]
[json.contains(a,"b")]

Returns true(1)

Check if the JSON Object a contains the key "z":

[h:a=json.fromStrProp("a=1;b=44;c=12")]
[json.contains(a,"z")]

Returns false(0)

Check if the JSON Array a contains the value "b":

[h:a=json.fromList("1,b,3,d")]
[json.contains(a,"b")]
Returns true(1)

See Also

Version Changes

  • 1.3b51 - Added ability for function to work with JSON Arrays.