labm8.jsonutil

JSON parser which supports comments.

labm8.jsonutil.format_json(data)

Pretty print JSON.

Parameters:data (dict) – JSON blob.
Returns:Formatted JSON
Return type:str
labm8.jsonutil.loads(text, **kwargs)

Deserialize text (a str or unicode instance containing a JSON document with Python or JavaScript like comments) to a Python object.

Supported comment types: // comment and # comment.

Taken from commentjson, written by Vaidik Kapoor.

Copyright (c) 2014 Vaidik Kapoor, MIT license.

Parameters:
  • text (str) – serialized JSON string with or without comments.
  • **kwargs (optional) – all the arguments that json.loads accepts.
Returns:

Decoded JSON.

Return type:

dict or list

labm8.jsonutil.read_file(*components, **kwargs)

Load a JSON data blob.

Parameters:
  • path (str) – Path to file.
  • must_exist (bool, otional) – If False, return empty dict if file does not exist.
Returns:

JSON data.

Return type:

array or dict

Raises:
  • File404 – If path does not exist, and must_exist is True.
  • InvalidFile – If JSON is malformed.
labm8.jsonutil.write_file(path, data, format=True)

Write JSON data to file.

Parameters:
  • path (str) – Destination.
  • data (dict or list) – JSON serializable data.
  • format (bool, optional) – Pretty-print JSON data.