Package tlib :: Package base :: Module JsonHelper :: Class JsonHelper
[hide private]
[frames] | no frames]

Class JsonHelper

source code

object --+
         |
        JsonHelper

Functions for modifying and validating JSON data

Instance Methods [hide private]

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __init__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__

Static Methods [hide private]
 
assert_json_equals(json1, json2, ignore_order=True, exclude_keys=[])
Compares two JSON values
source code
 
_validate_json_ignoring_order(json1, json2, exclude_keys)
Compares two JSON values, ignoring position of keys
source code
 
_validate_json_with_order(json1, json2, exclude_keys=[])
Compares two JSON values, taking into account key order If json1 or json2 are strings, it must be a valid JSON string
source code
 
assert_equal(dictA, dictB)
Takes two dictionaries, compares and displays any distinct keys in both, matching keys but different values, matching key/values @param dictA: dictionary or tuple (for e.g.
source code
 
remove_keys(data, keys, top_level=True)
Takes a dictionary or a list and removes keys specified by 'keys' parameter
source code
 
_remove_key(data, key, top_level=True)
Takes a dictionary or a list and removes keys specified by 'keys' parameter
source code
 
get_elements(data, json_path)
" Applies a JSON path to a JSON string and returns the resulting node
source code
Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

assert_json_equals(json1, json2, ignore_order=True, exclude_keys=[])
Static Method

source code 

Compares two JSON values

If parameter validate_order is True, position of keys is taken into account during validation

If json1 or json2 are strings, it must be a valid JSON string

Parameters:
  • json1 (str, dict) - String or dictionary to compare
  • json2 (str, dict) - String or dictionary to compare
  • ignore_order (bool) - If true, order of keys is not validated
  • exclude_keys (list) - Keys to exclude from comparison, in the format 'key2.key3[2].key1'
Raises:
  • AssertionError - JSON values doesn't match

_validate_json_ignoring_order(json1, json2, exclude_keys)
Static Method

source code 

Compares two JSON values, ignoring position of keys

If json1 or json2 are strings, it must be a valid JSON string

Parameters:
  • json1 (str, dict) - String or dictionary to compare
  • json2 (str, dict) - String or dictionary to compare
Raises:
  • AssertionError - JSON values doesn't match

_validate_json_with_order(json1, json2, exclude_keys=[])
Static Method

source code 

Compares two JSON values, taking into account key order If json1 or json2 are strings, it must be a valid JSON string

Parameters:
  • json1 (str) - String to compare
  • json2 (str) - String to compare
  • exclude_keys (list) - Keys to exclude from comparison, in the format 'key2.key3[2].key1'
Raises:
  • AssertionError - JSON values doesn't match

assert_equal(dictA, dictB)
Static Method

source code 

        Takes two dictionaries, compares and displays any distinct keys in both, matching keys but different values, matching key/values
        @param dictA: dictionary or tuple (for e.g. expected dictionary)
        @type dictA: dictionary
        @param dictB: dictionary or tuple (for e.g. dictionary captured at run time)
        @type dictA: dictionary
        e.g. Tuple: dictA = {Dictionary1, Dictionary2, Dictionary3} = {'city': ['montreal'], 'ln': ['en']}, {'drink': ['water'], 'weather': ['rainy']}, {'device': ['iPad']}
                    dictB = {Dictionary1, Dictionary2, Dictionary3} = {'city': ['montreal'], 'ln': ['fr'], 'color':['blue']}, {'drink': ['alcohol'], 'weather': ['rainy']}, {'device': ['iPad']}
            Output: (prints only the non matching dictionaries with the differences)
            >           assert False, '
'.join(nonmatching_dict)
            E           AssertionError:
            E
            E           Dictionary_1
            E           distinct_tags_in_dict_1A: None
            E           distinct_tags_in_dict_1B: {color:['blue']}
            E           nonmatching_tags_in_two_dicts:{ln:['en']} & {ln:['fr']} respectively
            E           matching_tags_in_two_dicts:{city:['montreal']}
            E
            E
            E           Dictionary_2
            E           distinct_tags_in_dict_2A: None
            E           distinct_tags_in_dict_2B: None
            E           nonmatching_tags_in_two_dicts:{drink:['water']} & {drink:['alcohol']} respectively
            E           matching_tags_in_two_dicts:{weather:['rainy']}
        

remove_keys(data, keys, top_level=True)
Static Method

source code 

Takes a dictionary or a list and removes keys specified by 'keys' parameter

Parameters:
  • data (dictionary, OrderedDict) - dictionary or OrderedDict that will be modified. Object is replaced in place
  • keys (list) - array indicating the path to remove. e.g. ['businessHours.headings[2]', 'businessHours.values.name']

_remove_key(data, key, top_level=True)
Static Method

source code 

Takes a dictionary or a list and removes keys specified by 'keys' parameter

Parameters:
  • data (dictionary, OrderedDict) - dictionary or OrderedDict that will be modified. Object is replaced in place
  • key (list) - array indicating the path to remove. e.g. ['businessHours', 'headings[2]']

get_elements(data, json_path)
Static Method

source code 

" Applies a JSON path to a JSON string and returns the resulting node

Parameters:
  • data (str, dict)