Hjson

3.1.0 · active · verified Thu Apr 09

Hjson is a syntax extension to JSON, designed as a human-friendly configuration file format. It allows for comments, multiline strings, and optional quotes, aiming to reduce common errors made by humans when writing JSON. The Python implementation, `hjson-py`, is based on `simplejson` and is currently at version 3.1.0.

Warnings

Install

Imports

Quickstart

Demonstrates how to parse Hjson strings into Python objects using `hjson.loads()` and how to serialize Python objects into Hjson or strict JSON strings using `hjson.dumps()` and `hjson.dumpsJSON()` respectively.

import hjson
import collections

# Decoding Hjson
hjson_text = '''
{
  foo: a # This is a comment
  bar: 1
  # Multiline string example
  description: '''
    This is a
    multiline string.
  '''
}
'''

# Parse Hjson string
data = hjson.loads(hjson_text)
print(f"Decoded Hjson: {data}")
assert data == collections.OrderedDict([
    ('foo', 'a'),
    ('bar', 1),
    ('description', 'This is a\nmultiline string.\n')
])

# Encoding Python object hierarchies to Hjson
python_obj = {'foo': 'text', 'bar': [1, 2], 'nested': {'key': 'value'}}
hjson_output = hjson.dumps(python_obj, indent=2)
print(f"\nEncoded Hjson:\n{hjson_output}")

# Encoding to strict JSON (note: may be less performant than simplejson)
json_output = hjson.dumpsJSON(python_obj, indent=2)
print(f"\nEncoded JSON:\n{json_output}")

view raw JSON →