Getting started
Installation
Install json4humans
as you would for any other Python package.
# with pip
$ pip install json4humans
# with pipenv
$ pipenv install json4humans
#with PDM
$ pdm add json4humans
TL;DR
All JSON4Humans modules implements the JSON Module protocol and so behave exactly like the Python builtin json module. Just import the desired syntax module and use it as you would with the builtin moodule.
from json4humans import json
DATA = """
{
"key": "value"
}
"""
# Loads data from string
data = json.loads(DATA)
# Manipulate using the native types interfaces
assert data["key"] == "value"
# Serialize with style preservation
assert json.dumps(data) == DATA
from json4humans import jsonc
DATA = """
{
// Comment
"key": "value"
}
"""
# Loads data from string
data = jsonc.loads(DATA)
# Manipulate using the native types interfaces
assert data["key"] == "value"
# Serialize with style preservation
assert jsonc.dumps(data) == DATA
from json4humans import json5
DATA = """
{
// Comment
key: "value"
}
"""
# Loads data from string
data = json5.loads(DATA)
# Manipulate using the native types interfaces
assert data["key"] == "value"
# Serialize with style preservation
assert json5.dumps(data) == DATA
The JSON Module protocol
All supported formats modules implements the JSON Module protocol which is trying to mimic as much as possible the Python builtin json module.
from json4humans import json, json5, jsonc
from json4humans.protocol import JSONModule
for format in json, jsonc, json5:
assert isinstance(format, JSONModule)