Skip to main content

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

Project description

Json Comment

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


  • Python 3.3+


  • ujson 1.30+


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.


  • #, ; 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


pip install jsoncomment


  • Download source
  • python install


from jsoncomment import JsonComment

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


Added in the /examples directory


  • #, ;, // 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 code available with MIT license on Bitbucket.


Added in top level

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


# 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)


Gaspare Iengo @ email

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

jsoncomment-0.4.2.tar.gz (9.2 kB view hashes)

Uploaded Source

Built Distribution

jsoncomment-0.4.2-py3-none-any.whl (6.8 kB view hashes)

Uploaded Python 3

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