json.path.read: Difference between revisions

From RPTools Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 3: Line 3:
|version=1.5.5
|version=1.5.5
|description=
|description=
Returns the values in a nested [[JSON Array]] or [[JSON Object]] corresponding to the provided path. For detailed information on how to specify the path, please read the [https://github.com/json-path/JsonPath following document].
Returns the values in a nested [[JSON Array]] or [[JSON Object]] corresponding to the provided path. It is unnecessary to include the root node operator {{code|$}} at the beginning of the requested path.  To do so, you must ''escape'' the dollar sign like this: {{code|\$.path.to.read}}.  For detailed information on how to specify the path, please read the [https://github.com/json-path/JsonPath following document].


|usage=
|usage=
Line 28: Line 28:
</source>
</source>


which returns "Sword".
which returns {{code|"Sword"}}.


If we instead wanted to return an array with the attacks of every monster, we could type
If we instead wanted to return an array with the attacks of every monster, we could type
Line 36: Line 36:
</source>
</source>


which would return [["Claw","Bite"],["Sword","Punch"]].
which would return {{code|[["Claw","Bite"],["Sword","Punch"]]}}.


Inline filters are also supported, so that if we want the name of the monsters with > 30 HPs, we can type
Inline filters are also supported, so that if we want the name of the monsters with > 30 HPs, we can type
Line 44: Line 44:
</source>
</source>


which returns ["Troll"].
which returns {{code|["Troll"]}}.


}}
}}

Revision as of 13:52, 28 September 2019

json.path.read() Function

Introduced in version 1.5.5
Returns the values in a nested JSON Array or JSON Object corresponding to the provided path. It is unnecessary to include the root node operator $ at the beginning of the requested path. To do so, you must escape the dollar sign like this: \$.path.to.read. For detailed information on how to specify the path, please read the following document.

Usage

json.path.read(json, path)

Parameters

  • json - The json element to get the values from.
  • path - The path of the values.

Examples

Suppose we have the following nested json:
[h:troll = json.set("{}", "name", "Troll", "HP", 75, "Attacks", json.append("Claw", "Bite"))]
[h:orc = json.set("{}", "name", "Orc", "HP", 13, "Attacks", json.append("Sword", "Punch"))]
[h:monsters = json.set("{}", "Troll", troll, "Orc", orc)]

To access the value of the first weapon of the Orc, we can type

[json.path.read(monsters, "Orc.Attacks.[0]")]

which returns "Sword".

If we instead wanted to return an array with the attacks of every monster, we could type

[r: json.path.read(monsters, ".Attacks")]

which would return [["Claw","Bite"],["Sword","Punch"]].

Inline filters are also supported, so that if we want the name of the monsters with > 30 HPs, we can type

[r: json.path.read(monsters, ".[?(@.HP > 30)].name")]
which returns ["Troll"].