Webinar

November 3rd, 2021 12:00 PM EDT
Automation for Field Services & DistributionNovember 3rd, 2021 12:00 PM EDT
Learn how creating a digital workforce can improve your supply chain processes!

Add to JSON

Add items into a JSON serializable object and return the result.

Arguments

Argument Type Default value Description
docDict[str, typing.Any], List[typing.Any], str, int, float, bool, Nonenull
exprstrnull
valueDict[str, typing.Any], List[typing.Any], str, int, float, bool, Nonenull

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"}]}
&{person}=    Create dictionary      Name=John
&{after}=     Add to JSON    ${before}   $.People    ${person}

Convert JSON to String

Convert a JSON serializable object to a string and return it.

Arguments

Argument Type Default value Description
docDict[str, typing.Any], List[typing.Any], str, int, float, bool, Nonenull
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
docstrnull
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
docDict[str, typing.Any], List[typing.Any], str, int, float, bool, Nonenull
exprstrnull

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    ${before}   $.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
docDict[str, typing.Any], List[typing.Any], str, int, float, bool, Nonenull
exprstrnull
defaultAny, NoneNone

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
docDict[str, typing.Any], List[typing.Any], str, int, float, bool, Nonenull
exprstrnull
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
filenamestrnull
encodingutf-8

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
param encoding:file character encoding

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
docDict[str, typing.Any], List[typing.Any], str, int, float, bool, Nonenull
filenamestrnull
indentint, NoneNone
encodingutf-8

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
param indent:if given this value is used for json file indent
param encoding:file character encoding

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
docDict[str, typing.Any], List[typing.Any], str, int, float, bool, Nonenull
exprstrnull
valueDict[str, typing.Any], List[typing.Any], str, int, float, bool, Nonenull

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     ${before}   $.People[*].Name    JohnMalkovich