conversion#

Core functions to convert data to different format

async idem_core_functions.exec.core.conversion.to_json(hub, data, skip_keys: bool = False, ensure_ascii: bool = True, check_circular: bool = True, allow_nan: bool = True, indent: int = None, separators: tuple = None, default: Any = None, sort_keys: bool = False) Dict[source]#

Serialize an object to a string of JSON.

Parameters:
  • data – It serializes the data as a JSON formatted.

  • skip_keys (bool, Optional) – The default parameter for skipkeys is False, but if it is True, then dict keys that are not of a basic type (str, int, float, bool, None) will be skipped instead of raising a TypeError.

  • ensure_ascii (bool, Optional) – The default value for ensure_ascii is True, and the output is assured to have all incoming non-ASCII characters escaped.

  • check_circular (bool, Optional) – The default value for check_circular is True and if it is False, then the circular reference check for container types will be skipped, and a circular reference will result in an OverflowError.

  • allow_nan (bool, Optional) – The default value for check_circular is True and if it is False, then it will be a ValueError to serialize out-of-range float values (nan, inf, -inf) in strict acquiescence with the JSON specification.

  • indent (int or str, Optional) –

    If indent is a non-negative integer or string, then JSON array elements and object members will be pretty-printed with that indent level.

    An indent level of 0, negative, or ” ” will only insert newlines. None (the default) selects the most compact representation.

    A positive integer indent indents that many spaces per level. If indent is a string (such as “ ”), that string is used to indent each level.

  • separators (tuple, Optional) – If specified, separators should be an (item_separator, key_separator) tuple. The default is (‘, ‘, ‘: ‘) if indent is None and (‘, ‘, ‘: ‘) otherwise. To get the most compact JSON representation, you should specify (‘, ‘, ‘:’) to trim whitespace.

  • default (Any, Optional) – If specified, the default should be a function called for objects that can’t otherwise be serialized. It should return a JSON encodable version of the object or raise a TypeError. If not specified, TypeError is raised.

  • sort_keys (bool, Optional) – The sort_keys parameter value is False by default, but if it is true, then the output of dictionaries will be sorted by key.

Returns:

{"result": True|False, "comment": list, "ret": None|dict}

Examples

Calling this exec module function from the cli (input data is str)

idem exec core.conversion.to_json data='{ "cluster_name": "idem-eks-test", "region": "ap-south-1" }'

Using in a state:

my_unmanaged_resource:
  exec.run:
    - path: core.conversion.to_json
    - kwargs:
        data: '{
          "cluster_name":  "idem-eks-test",
          "region": "ap-south-1",
        }'
async idem_core_functions.exec.core.conversion.json_to_dict(hub, data: str, parse_float: str = None, parse_int: str = None, parse_constant: str = None, object_pairs_hook: str = None)[source]#

Convert the JSON String document into the Python dictionary

Parameters:
  • data (str) – string that needs to be converted to Python dictionary

  • parse_float (str, Optional) – if specified, will be called with the string of every JSON float to be decoded. By default, this is equivalent to float(num_str). This can be used to use another datatype or parser for JSON floats (e.g. decimal.Decimal)

  • parse_int (str, Optional) – if specified, will be called with the string of every JSON int to be decoded. By default, this is equivalent to int(num_str). This can be used to use another datatype or parser for JSON integers (e.g. float).

  • parse_constant (str, Optional) – if specified, will be called with one of the following strings: ‘-Infinity’, ‘Infinity’, ‘NaN’. This can be used to raise an exception if invalid JSON numbers are encountered.

  • object_pairs_hook (str, Optional) – object_pairs_hook is an optional function that will be called with the result of any object literal decoded with an ordered list of pairs. The return value of object_pairs_hook will be used instead of the dict. This feature can be used to implement custom decoders. If object_hook is also defined, the object_pairs_hook takes priority.

Returns:

{"result": True|False, "comment": list, "ret": None|dict}

Examples

Using in a state:

Idem-state-name:
  exec.run:
    - path: core.conversion.to_json
    - kwargs:
        data: '{"test1": "test1_val", "test2": "test2_val"}'
async idem_core_functions.exec.core.conversion.yaml_to_dict(hub, data: str)[source]#

Convert the YAML String document into the Python dictionary.

Parameters:

data (str) – string that needs to be converted to Python dictionary

Returns:

{"result": True|False, "comment": list, "ret": None|dict}

Examples

Using in a state:

Idem-state-name:
  exec.run:
    - path: core.conversion.yaml_to_dict
    - kwargs:
        data:
          names:
            - name1: test1
            - name2: test2