Skip to main content

Adding import functionality to JSON files

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.1.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

importsinjson-0.1.1-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: importsinjson-0.1.1.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.1 CPython/3.10.6 Linux/5.15.0-1024-azure

File hashes

Hashes for importsinjson-0.1.1.tar.gz
Algorithm Hash digest
SHA256 9e21bef4c889e8d41e87e99e137f61afb4eb0400e67314c613267eeba2223af5
MD5 574caf4eab69053fc3a03f0b6a8a15fc
BLAKE2b-256 7e0759cc9731e8a90a02a101bafe6b643845a7f02367c39b2c2a0761419e24a6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: importsinjson-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 5.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.1 CPython/3.10.6 Linux/5.15.0-1024-azure

File hashes

Hashes for importsinjson-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 09607de9d9dd25509cf90b219c91c86dd4dd90ec16f86850575e3193eaec377e
MD5 8a92247a50b274c3de950f27448ae624
BLAKE2b-256 abc67020b77a2ed2a5e946f3f34e59ff14477df28899ec1a4da8c130f623058e

See more details on using hashes here.

Supported by

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