JsonValidator

Library version:1.0.0
Library scope:global
Named arguments:supported

Introduction

Library for JSON validation. Based on: JSONSchema, JSONPath, JSONSelect.

Additional Information

Dependencies

jsonschema https://pypi.python.org/pypi/jsonschema
jsonpath-rw-ext https://pypi.python.org/pypi/jsonpath-rw-ext
objectpath https://pypi.python.org/pypi/objectpath/
pyjsonselect https://pypi.python.org/pypi/pyjsonselect

Example of use

json_example.json

{ "store": {
       "book": [
         { "category": "reference",
           "author": "Nigel Rees",
           "title": "Sayings of the Century",
           "price": 8.95
         },
         { "category": "fiction",
           "author": "Evelyn Waugh",
           "title": "Sword of Honour",
           "price": 12.99
         },
         { "category": "fiction",
           "author": "Herman Melville",
           "title": "Moby Dick",
           "isbn": "0-553-21311-3",
           "price": 8.99
         },
         { "category": "fiction",
           "author": "J. R. R. Tolkien",
           "title": "The Lord of the Rings",
           "isbn": "0-395-19395-8",
           "price": 22.99
         }
       ],
       "bicycle": {
         "color": "red",
         "price": 19.95
       }
   }
}
Settings Value
Library JsonValidator
Library OperatingSystem
Test Cases Action Argument Argument
Check element ${json_example}= OperatingSystem.Get File ${CURDIR}${/}json_example.json
Element should exist ${json_example} .author:contains("Evelyn Waugh")

Shortcuts

Element Should Exist · Element Should Not Exist · Get Elements · Json To String · Pretty Print Json · Select Elements · Select Objects · String To Json · Update Json · Validate Jsonschema · Validate Jsonschema From File

Keywords

Keyword Arguments Documentation
Element Should Exist json_string, expr

Check the existence of one or more elements, matching JSONSelect expression.

DEPRECATED JSON Select query language is outdated and not supported any more. Use other keywords of this library to query JSON.

Args:

json_string - JSON string;

expr - JSONSelect expression;

Raises:

JsonValidatorError

Example:

Settings Value
Library JsonValidator
Library OperatingSystem
Test Cases Action Argument Argument
Check element ${json_example}= OperatingSystem.Get File ${CURDIR}${/}json_example.json
Element should exist ${json_example} .author:contains("Evelyn Waugh")
Element should exist ${json_example} .store .book .price:expr(x=8.95)
Element Should Not Exist json_string, expr

Check that one or more elements, matching JSONSelect expression, don't exist.

DEPRECATED JSON Select query language is outdated and not supported any more. Use other keywords of this library to query JSON.

Args:

json_string - JSON string;

expr - JSONSelect expression;

Raises:

JsonValidatorError

Get Elements json_string, expr

Get list of elements from json_string, matching JSONPath expression.

Args:

json_string - JSON string;

expr - JSONPath expression;

Returns:

List of found elements or None if no elements were found

Example:

Settings Value
Library JsonValidator
Library OperatingSystem
Test Cases Action Argument Argument
Get json elements ${json_example}= OperatingSystem.Get File ${CURDIR}${/}json_example.json
${json_elements}= Get elements ${json_example} $.store.book[*].author

=>

[u'Nigel Rees', u'Evelyn Waugh', u'Herman Melville', u'J. R. R. Tolkien']
Json To String source

Serialize JSON structure into string.

Args:

source - JSON structure

Returns:

JSON string

Raises:

JsonValidatorError

Example:

Settings Value
Library JsonValidator
Library OperatingSystem
Test Cases Action Argument Argument
Json to string ${json_string}= OperatingSystem.Get File ${CURDIR}${/}json_example.json
${json}= String to json ${json_string}
${string}= Json to string ${json}
${pretty_string}= Pretty print json ${string}
Log to console ${pretty_string}
Pretty Print Json json_string

Return formatted JSON string json_string.

Using method json.dumps with settings: indent=2, ensure_ascii=False.

Args:

json_string - JSON string.

Returns:

Formatted JSON string.

Example:

Settings Value
Library JsonValidator
Library OperatingSystem
Test Cases Action Argument Argument
Check element ${pretty_json}= Pretty print json {a:1,foo:[{b:2,c:3},{d:"baz",e:4}]}
Log ${pretty_json}

=>

{
   "a": 1,
   "foo": [
     {
       "c": 3,
       "b": 2
     },
     {
       "e": 4,
       "d": "baz"
     }
   ]
}
Select Elements json_string, expr

Return list of elements from json_string, matching JSONSelect expression.

DEPRECATED JSON Select query language is outdated and not supported any more. Use other keywords of this library to query JSON.

Args:

json_string - JSON string;

expr - JSONSelect expression;

Returns:

List of found elements or None if no elements were found

Example:

Settings Value
Library JsonValidator
Library OperatingSystem
Test Cases Action Argument Argument
Select json elements ${json_example}= OperatingSystem.Get File ${CURDIR}${/}json_example.json
${json_elements}= Select elements ${json_example} .author:contains("Evelyn Waugh")~.price

=>

12.99
Select Objects json_string, expr

Return list of elements from json_string, matching ObjectPath expression.

Args:

json_string - JSON string;

expr - ObjectPath expression;

Returns:

List of found elements. If no elements were found, empty list will be returned

Example:

Settings Value
Library JsonValidator
Library OperatingSystem
Test Cases Action Argument Argument
Select json objects ${json_example}= OperatingSystem.Get File ${CURDIR}${/}json_example.json
${json_objectss}= Select objects ${json_example} $..book[@.author.name is "Evelyn Waugh"].price

=>

[12.99]
String To Json source

Deserialize string into JSON structure.

Args:

source - JSON string

Returns:

JSON structure

Raises:

JsonValidatorError

Example:

Settings Value
Library JsonValidator
Library OperatingSystem
Test Cases Action Argument Argument
String to json ${json_string}= OperatingSystem.Get File ${CURDIR}${/}json_example.json
${json}= String to json ${json_string}
Log ${json["store"]["book"][0]["price"]}

=>

8.95

Update Json json_string, expr, value, index=0

Replace the value in the JSON string.

Args:

json_string - JSON string;

expr - JSONPath expression for determining the value to be replaced;

value - the value to be replaced with;

index - index for selecting item within a match list, default value is 0;

Returns:

Changed JSON in dictionary format.

Example:

Settings Value
Library JsonValidator
Library OperatingSystem
Test Cases Action Argument Argument
Update element ${json_example}= OperatingSystem.Get File ${CURDIR}${/}json_example.json
${json_update}= Update_json ${json_example} $..color changed
Validate Jsonschema json_string, input_schema

Validate JSON according to schema.

Args:

json_string - JSON string;

input_schema - schema in string format;

Raises:

JsonValidatorError

Example:

Settings Value
Library JsonValidator
Library OperatingSystem
Test Cases Action Argument Argument
Simple ${schema}= OperatingSystem.Get File ${CURDIR}${/}schema_valid.json
Validate jsonschema {"foo":bar} ${schema}
Validate Jsonschema From File json_string, path_to_schema

Validate JSON according to schema, loaded from a file.

Args:

json_string - JSON string;

path_to_schema - path to file with JSON schema;

Raises:

JsonValidatorError

Example:

Settings Value
Library JsonValidator
Test Cases Action Argument Argument
Simple Validate jsonschema from file {"foo":bar} ${CURDIR}${/}schema.json