Skip to main content

Json Comment

A wrapper to JSON parsers allowing comments, multiline strings and trailing commas


Dependencies

  • Python 3.3+
  • TODO NEW ADDITIONS

Optional

  • ujson 1.30+

Description

JSON Comment allows to parse JSON files or strings with:

  • Single and Multi line comments
  • Multi line data strings
  • Trailing commas in objects and arrays, after the last item

This package works with any JSON parser which supports:

  • load(fp, ...) to parse files
  • loads(s, ...) to parse strings

by adding a preprocessor to these calls.


Comments

  • #, ; and // are for single line comments
  • /* and */ enclose multiline comments

Inline comments are not supported


Multiline strings

Any string can be multiline, even object keys.

  • Multiline strings start and end with """, like in python
  • The preprocessor merges all lines to a single JSON standard string
  • A single trailing space per line is kept, if present
  • New line is not kept. To hard code new lines in the string, use \\n

Custom Methods

File Load

loadf(path, *args, default = None, **kwargs)

Opens a JSON file with comments. Allows a default value if loading or parsing fails

File Save

dumpf(json_obj, path, *args, indent=4, **kwargs)

Saves a JSON file with indentation


Install

pip install jsoncomment

OR

  • Download source
  • python setup.py install

Usage

from jsoncomment import JsonComment

string = "[]"
json = JsonComment()
json_obj = json.loads(string)

Examples

Added in the /examples directory


Limitations

  • #, ;, // and /* may be preceded only by whitespaces or tabs on the same line
  • */ may be followed only by whitespaces or tabs on the same line
  • The trailing comma must be the last character on the same line

Source

Source code available with MIT license on Bitbucket.


API

Added in top level __init__.py

How to read the API

API is split in:

* `User Interface` for common use
* `Developer Interface` exposing some internals that could be useful

For each item ( function or class ), there are 2 blocks of comments, above and below item definition:

* The top describes the return values
* The bottom describes the item and call variables

If call variables have defaults or use duck typing, every allowed value is described

Example:

# return_value
	# description
from .some_module import SomeClass
	# SomeClass description
# (
	# variable_1,
		# description
	# variable_2 = something,
		# description

		# = Default
			# description of default value ( something )
		# = something_2
			# description of alternate form ( duck typing )
# )

describes return_value = SomeClass(variable_1, variable_2 = current_value)


Contact

Gaspare Iengo @ email

Release files for jsoncomment 0.4.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for jsoncomment 0.4.2
File Size Uploaded
jsoncomment-0.4.2.tar.gz 9.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for jsoncomment 0.4.2
File Interpreter ABI Platform
jsoncomment-0.4.2-py3-none-any.whl Python 3 none any Details

Total release size: 16.0 kB

Release files / jsoncomment-0.4.2.tar.gz

Download URL jsoncomment-0.4.2.tar.gz
Size 9.2 kB
Tags Source
SHA-256 checksum
How to use checksums
8fa065a85327306211ec567bb9ad8ca252c59332f6c6cf09c3228a0abf1191da
BLAKE2b-256 checksum
How to use checksums
c375b5c7895b8d2edcecbc49b55adb10087183050f95eac33c3453cc12c610f1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.7.0 requests-toolbelt/0.8.0 tqdm/4.30.0 CPython/3.7.2

Release files / jsoncomment-0.4.2-py3-none-any.whl

Download URL jsoncomment-0.4.2-py3-none-any.whl
Size 6.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7ce82b755e54fdc0dc1c3c05e2bc7db0c3f609be31771401f78f00e743b0403e
BLAKE2b-256 checksum
How to use checksums
139eec3da1601c7a5700e9e4229d5404e590107a1bbe50c78dbcfb33948177fc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.7.0 requests-toolbelt/0.8.0 tqdm/4.30.0 CPython/3.7.2

Release history Release notifications | RSS feed

This release

0.4.2 This release

2 release files

0.4.1

2 release files

0.3.3

2 release files

0.3.2

2 release files

0.3.0

3 release files

0.2.3

3 release files

0.2.2

3 release files

0.2.1

3 release files

0.2.0

3 release files

0.1.7

3 release files

0.1.6

3 release files

0.1.5

3 release files

0.1.4

3 release files

0.1.3

1 release file

0.1.2

3 release files

0.1.1

3 release files

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page