Skip to main content

Adding import functionality to JSON files

This project has been archived.

The maintainers of this project have marked this project as archived. No new releases are expected.

Project description

ImportsInJSON

Black GitHub license PyPi versions PyPi downloads

Python JSON Import Library


ImportsInJSON 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.

Installation

ImportsInJSON requires Python 3.7+.

pip install importsinjson

If you'd like to support loading JSON files with comments, either add the commentjson or pyjson5 extra when installing.

pip install importsinjson[commentjson]
# or
pip install importsinjson[pyjson5]

Usage

In your Python code, import importsinjson is a drop-in replacement for the json module.

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 $import with a string value that points to another file. Let's say you have a JSON file a.json:

{
  "name": "John Doe",
  "age": 30,
  "$import": "b.json"
}

and

{
  "profession": "Engineer"
}

Running importsinjson.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.json would 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"]
}

Options

Depending on the JSON parsing backend selected, options do vary. However, for all backends, the following options are available:

  • strict: Defaults to False. If True, will raise a FileNotFoundError if the imported filepath cannot be found. Additionally, if the specified key/index could not be found, the KeyError, IndexError, etc will be raised. If False, the original value will be kept instead.

Default

The default JSON parsing backend is the Python standard library json module. All normal options for this can be used.

CommentJSON

Homepage

pip install importsinjson[commentjson]

If installed, commentjson will be used as the JSON parsing backend. This is a pure-Python implementation that strips comments from JSON data before handing them to the json module. This also supports all options of the json module. However, it does not support multi-line comments.

PyJSON5

Homepage

pip install importsinjson[pyjson5]

Lastly, if installed, pyjson5 will 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. See here When using this, you may need to explictly add encoding="utf-8" to the load and loads functions.

If for some reason you want to change the prefix used to import data, you can set that like so:

import importsinjson
importsinjson.PREFIX = "$newimportsymbol"

Gotchas

Using the load function is much preferred over the loads function. 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.

With load imported file paths can be either:

  • absolute
  • relative to the parent file
  • relative to the current working directory

With loads imported file paths can only be:

  • absolute
  • relative to the current working directory

Additionally, this module also works recursively, so make sure not to create an infinite loop.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

importsinjson-0.1.0.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

importsinjson-0.1.0-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file importsinjson-0.1.0.tar.gz.

File metadata

  • Download URL: importsinjson-0.1.0.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.10.4 Linux/5.13.0-1023-azure

File hashes

Hashes for importsinjson-0.1.0.tar.gz
Algorithm Hash digest
SHA256 a9f825118b1ce569028fd9243d4866a0c67201ddb2da1a1be74a339bec5d67b4
MD5 aa8d34ec6d4a33749d0e45b6e8b1e3da
BLAKE2b-256 04b968ffa4b33cffecfc153567a737c75ecbb05a2386b45b294bc38d74a03a5c

See more details on using hashes here.

File details

Details for the file importsinjson-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: importsinjson-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.10.4 Linux/5.13.0-1023-azure

File hashes

Hashes for importsinjson-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8709f705cc9eb1c8502577b5cd0cfc7cdc5221927127d016261a426b5c8b155b
MD5 e16c496828bbed01dd5684f338692209
BLAKE2b-256 f4742998dd5bdf13f5bca95d5198e59b7d77b4b70cad8f8adff786dc296bd8d1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page