jsontas package

Submodules

jsontas.dataset module

Dataset module.

class jsontas.dataset.Dataset[source]

Bases: object

JSONTas dataset object. Used for lookup of $ notated strings in a JSON file.

add(key, value)[source]

Add a new dataset value and key.

Parameters:
  • key (str) – Dictionary key for global dataset dict.
  • value (any) – Dictionary value for global dataset dict.
copy()[source]

Make a copy of this dataset.

Returns:A dataset object with a copy of the internal dataset in it.
Return type:Dataset
get(key, default=None)[source]

Get a key from dataset global dictionary.

Parameters:
  • key (any) – Key to get from global dataset dictionary.
  • default (any) – Default value if the key does not exist.
Returns:

Value from key in global dataset dictionary or default.

Return type:

any

get_or_getattr(datasubset, key)[source]

Get a key from a datasubset. Either using ‘get’ or ‘getattr’.

Raises:

AttributeError if attribute could not be found with either ‘get’ or ‘getattr’.

Parameters:
  • datasubset (any) – Dataset to attempt to get key from.
  • key (any) – Key to get.
Returns:

Value from key.

Return type:

any

logger = <Logger Dataset (WARNING)>
lookup(query_string, parameters)[source]

Lookup JSONTas query string against dataset.

Query string is a dot separated string of keywords which is split and iterated over.

If the query string is “$something.another.third”:

# Iterate through the words
["something", "another", "third"]

These keywords are then matched against the dataset very simply

  1. Try to get keyword from dataset, either via “.get” or “getattr”.
  2. If keyword exists, inspect what the type of the keyword value is.
  3. If it’s a jsontas.data_structures.datastructure.DataStructure, call its ‘execute’ method with the parameters and keyword value.
  4. If it’s a function, call the function with the parameters and keyword value.
  5. If neither of the above are True, set dataset to the value.
  6. Loop to the next keyword.

If at any point of this, the keyword value becomes None, return without changing the key. (the key is, by default, the query_string)

If the keyword value is not None after the loop is finished, return the key and value. Note that the jsontas.data_structures.datastructure.DataStructure and functions must return a key, value pair.

Value will be put as the ‘value’ in the JSON dict when returned, key is what the key will be changed to.

Assume the following dataset for the JSON below:

{
    "query": {
        "call": "Chaos"
    }
}

Input:

{
    "Text": "$query.call"
}

Output:

{
    "Text": "Chaos"
}

In most cases, as well as in these examples, the key returned is None. It is expected that the caller handles this situation by setting the correct key when None is returned.

The “correct” key is generally the key that was already there. In the examples above the the key is “Text”, and this method returns None, so JSONTas will keep the key as ‘Text’

However, if the key returned is not None, the key is overwritten by JSONTas. This is not a very common use-case, but it is possible to implement in a jsontas.data_structures.datastructure.DataStructure

This should be used with care! In fact, the only built-in use-case for this behavior is when there’s an exception in the lookup or the keyword value becomes None at any point in order to have the key stay as the query_string in case of problems.

Parameters:
  • query_string (str) – JSONTas query string.
  • parameters (any) – Parameters that exist nested below the query_string in JSON data. For instance {“$querystring”: {“some”: “data”}} parameters would be {“some”: “data”}
Returns:

New key and value as defined by dataset.

Return type:

tuple

merge(dataset)[source]

Merge a dataset with this dataset.

Parameters:dataset (dict) – Dataset to merge with this one.
regex = re.compile('[\\$\\-\\w!,:]+')

jsontas.jsontas module

JSONTas module.

class jsontas.jsontas.JsonTas(dataset=None)[source]

Bases: object

JSONTas resolver.

logger = <Logger JSONTas (WARNING)>
resolve(json_data, query_tree=None)[source]

Resolve JSONTas queries. Takes a JSON structure and resolve all values against dataset.

This is a recursive method.

Parameters:
  • json_data (any) – JSON data to iterate through and resolve.
  • query_tree (any) – Used in recursion to keep track of query_tree.
Returns:

New JSON structure with resolved values.

Return type:

any

run(json_data=None, json_file=None, copy=True)[source]

Run JSONTas. This should be the main entry to JSONTas.

Parameters:
  • json_data (file) – JSON data to run JSONTas on.
  • json_file – JSON file to run JSONTas on.
Returns:

Resolved JSON structure.

Return type:

OrderedDict

Module contents

Distribution name and version.