Add to JSON
Add items into a JSON serializable object and return the result.
Arguments
Argument | Type | Default value | Description |
---|---|---|---|
doc | typing.Union[str, int, float, bool, NoneType, typing.Dict[str, typing.Any], typing.List[typing.Any]] | JSON serializable object | |
expr | str | JSONPath expression | |
value | typing.Union[str, int, float, bool, NoneType, typing.Dict[str, typing.Any], typing.List[typing.Any]] | values to either append or update |
If the target is a list, the values are appended to the end. If the target is a dict, the keys are either added or updated.
param doc: | JSON serializable object |
---|---|
param expr: | JSONPath expression |
param value: | values to either append or update |
Examples
*** Tasks ***
# Change the name value for all people
&{before}= Convert string to JSON {"People": [{"Name": "Mark"}, {"Name": "Jane"}]}
&{after}= Add to JSON ${json} $.People.name JohnMalkovich
Convert JSON to String
Convert a JSON serializable object to a string and return it.
Arguments
Argument | Type | Default value | Description |
---|---|---|---|
doc | typing.Union[str, int, float, bool, NoneType, typing.Dict[str, typing.Any], typing.List[typing.Any]] | JSON serializable object |
param doc: | JSON serializable object |
---|
Examples
*** Tasks ***
${obj}= Create dictionary Key=Value
${json}= Convert JSON to string ${obj}
Should be equal ${json} {"Key": "Value"}
Convert String to JSON
Convert a string to a JSON serializable object and return it.
Arguments
Argument | Type | Default value | Description |
---|---|---|---|
doc | str | JSON string |
param doc: | JSON string |
---|
Examples
*** Tasks ***
${json}= Set variable {"Key": "Value"}
&{obj}= Convert string to JSON ${json}
Should be equal ${obj.Key} Value
Delete from JSON
Delete values from a JSON serializable object and return the result. Will delete all values that match the expression.
Arguments
Argument | Type | Default value | Description |
---|---|---|---|
doc | typing.Union[str, int, float, bool, NoneType, typing.Dict[str, typing.Any], typing.List[typing.Any]] | JSON serializable object or string | |
expr | str | JSONPath expression |
Delete values from a JSON serializable object and return the result. Will delete all values that match the expression.
param doc: | JSON serializable object or string |
---|---|
param expr: | JSONPath expression |
Examples
*** Tasks ***
# Delete all people
&{before}= Convert string to JSON {"People": [{"Name": "Mark"}, {"Name": "Jane"}]}
&{after}= Delete from JSON ${json} $.People[*]
Get value from JSON
Get a single value from a JSON serializable object that matches the given expression.
Arguments
Argument | Type | Default value | Description |
---|---|---|---|
doc | typing.Union[str, int, float, bool, NoneType, typing.Dict[str, typing.Any], typing.List[typing.Any]] | JSON serializable object or string | |
expr | str | jsonpath expression | |
default | typing.Any | None |
Raises a ValueError if there is more than one match. Returns the given default argument (or None) if there were no matches.
param doc: | JSON serializable object or string |
---|---|
param expr: | jsonpath expression |
Examples
*** Tasks ***
# Get the name value for the first person
&{people}= Convert string to JSON {"People": [{"Name": "Mark"}, {"Name": "Jane"}]}
${first}= Get value from JSON ${people} $.People[0].name
Get values from JSON
Get all values from a JSON serializable object that match the given expression.
Arguments
Argument | Type | Default value | Description |
---|---|---|---|
doc | typing.Union[str, int, float, bool, NoneType, typing.Dict[str, typing.Any], typing.List[typing.Any]] | JSON serializable object or string | |
expr | str | JSONPath expression |
param doc: | JSON serializable object or string |
---|---|
param expr: | JSONPath expression |
Examples
*** Tasks ***
# Get all the names for all people
&{people}= Convert string to JSON {"People": [{"Name": "Mark"}, {"Name": "Jane"}]}
@{names}= Get values from JSON ${people} $.People[*].name
Load JSON from file
Load JSON data from a file, and return it as JSON serializable object. Depending on the input file the object can be either a dictionary, a list, or a scalar value.
Arguments
Argument | Type | Default value | Description |
---|---|---|---|
filename | str | path to input file |
Load JSON data from a file, and return it as JSON serializable object. Depending on the input file the object can be either a dictionary, a list, or a scalar value.
param filename: | path to input file |
---|
Examples
*** Tasks ***
&{auth}= Load JSON from file auth.json
Log Current auth token: ${auth.token}
Save JSON to file
Save a JSON serializable object or a string containg a JSON value into a file.
Arguments
Argument | Type | Default value | Description |
---|---|---|---|
doc | typing.Union[str, int, float, bool, NoneType, typing.Dict[str, typing.Any], typing.List[typing.Any]] | JSON serializable object or string | |
filename | str | path to output file |
Save a JSON serializable object or a string containg a JSON value into a file.
param doc: | JSON serializable object or string |
---|---|
param filename: | path to output file |
Examples
*** Tasks ***
# Save dictionary to file
${john}= Create dictionary name=John mail=john@example.com
Save JSON to file ${john} john.json
# Save string to file
${mark}= Set variable {"name": "Mark", "mail": "mark@example.com"}
Save JSON to file ${mark} mark.json
Update value to JSON
Update existing values in a JSON serializable object and return the result. Will change all values that match the expression.
Arguments
Argument | Type | Default value | Description |
---|---|---|---|
doc | typing.Union[str, int, float, bool, NoneType, typing.Dict[str, typing.Any], typing.List[typing.Any]] | JSON or string | |
expr | str | JSONPath expression | |
value | typing.Union[str, int, float, bool, NoneType, typing.Dict[str, typing.Any], typing.List[typing.Any]] | New value for the matching item(s) |
Update existing values in a JSON serializable object and return the result. Will change all values that match the expression.
param doc: | JSON or string |
---|---|
param expr: | JSONPath expression |
param value: | New value for the matching item(s) |
Examples
*** Tasks ***
# Change the name key for all people
&{before}= Convert string to JSON {"People": [{"Name": "Mark"}, {"Name": "Jane"}]}
&{after}= Update value to JSON ${json} $.People[*].name JohnMalkovich