json-source-map

raw JSON →
1.0.5 verified Fri May 01 auth: no python

Calculate the source map for a JSON document, mapping JSON values to their exact source locations (line, column, path). Version 1.0.5 supports Python 3.8-3.11. Low activity; last release March 2022.

pip install json-source-map
error TypeError: expected string or bytes-like object
cause Passing a non-string (e.g., file object) as the second argument to `locate`.
fix
Read the file and pass the string: with open('file.json') as f: json_str = f.read(); result = locate(data, json_str)
error KeyError: 'value'
cause Treating the source map object as if it holds the original value under key 'value'. The correct field is 'value' but only after calling `locate`. Ensure you are using the returned object correctly.
fix
Each source map object has attributes: .value, .key, .value_line, .value_col, etc. Access via dot notation: result['key'].value.
gotcha The `locate` function transforms the JSON data. The returned dict contains source map objects, not the original values. To access original values, use the keys from the input data.
fix Access the source map via `result['key'].value` or iterate over original data keys.
gotcha The `locate` function expects a JSON string as second argument. Passing a file object or bytes will raise a TypeError.
fix Always pass a string. For bytes, decode first: `json_str = file_obj.read().decode('utf-8')`.
deprecated Python 3.7 support was dropped in v1.0.3. Installing on Python 3.7 will get v1.0.2 or fail.
fix Upgrade to Python 3.8+ or pin to json-source-map==1.0.2.

Parses a JSON string and returns source map for each key/value.

import json
from json_source_map import locate

json_str = '{"key": "value"}'
data = json.loads(json_str)
result = locate(data, json_str)
print(result['key'])