Package: importsinjson
ImportsInJSONPython JSON Import LibraryImportsInJSON is an easy way to allow Python to load JSON files that import data
from other JSON files. This is very helpful for splitting up large JSON files
into smaller chunks that can still be combined.InstallationImportsInJSON requires Python 3.7+.pipinstallimportsinjsonIf you'd like to support loading JSON files with comments, either add
thecommentjsonorpyjson5extra when installing.pipinstallimportsinjson[commentjson]# orpipinstallimportsinjson[pyjson5]UsageIn your Python code,import importsinjsonis a drop-in replacement for thejsonmodule.In your JSON document, there are 2 ways to import data from other JSON files.The first way is to simply have a key called$importwith a string value
that points to another file. Let's say you have a JSON filea.json:{"name":"John Doe","age":30,"$import":"b.json"}and{"profession":"Engineer"}Runningimportsinjson.load('a.json')will return the following:{"name":"John Doe","age":30,"profession":"Engineer"}This does observe Python's dictionary merging rules, so any keys in the imported
JSON file that are also in the original document will replace the values in the
original document.The second way to import data is to have a string value that starts with$import:and has the path to the file to import. Modifying the example from before,a.jsonwould become{"name":"John Doe","age":30,"profession":"$import:b.json"}However this would load the following:{"name":"John Doe","age":30,"profession":{"profession":"Engineer"}}To prevent duplicate keys like this, you can add another:after the file path,
and provide a path to the key in the imported file to use.{"name":"John Doe","age":30,"profession":"$import:b.json:/profession/"}This value can be anywhere in the JSON document, including in a list:{"name":"John Doe","age":30,"professions":["$import:b.json","$import:c.json"]}OptionsDepending on the JSON parsing backend selected, options do vary.
However, for all backends, the following options are available:strict: Defaults toFalse. IfTrue, will raise aFileNotFoundErrorif the imported filepath cannot be found. Additionally, if the specified key/index
could not be found, theKeyError,IndexError, etc will be raised. IfFalse,
the original value will be kept instead.DefaultThe default JSON parsing backend is the Python standard libraryjsonmodule.
All normal options for this can be used.CommentJSONHomepagepipinstallimportsinjson[commentjson]If installed,commentjsonwill be used as the JSON parsing backend.
This is a pure-Python implementation that strips comments from JSON data before
handing them to thejsonmodule. This also supports all options of thejsonmodule.
However, it does not support multi-line comments.PyJSON5Homepagepipinstallimportsinjson[pyjson5]Lastly, if installed,pyjson5will be used as the JSON parsing backend.
This is a Cython implementation that loads JSON data with comments. This is the most
restrictive backend, with very basic options.
SeehereWhen using this, you may need to explictly addencoding="utf-8"to theloadandloadsfunctions.If for some reason you want to change the prefix used to import data, you can set
that like so:importimportsinjsonimportsinjson.PREFIX="$newimportsymbol"GotchasUsing theloadfunction is much preferred over theloadsfunction.
This is because with a file handle, the path of the file can be used as an additional
search directory when looking for imported files.Withloadimported file paths can be either:absoluterelative to the parent filerelative to the current working directoryWithloadsimported file paths can only be:absoluterelative to the current working directoryAdditionally, this module also works recursively, so make sure not to create an
infinite loop.