Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse json file with c-style comments?

Tags:

python

json

I have a json file, such as the following:

    {         "author":"John",        "desc": "If it is important to decode all valid JSON correctly \  and  speed isn't as important, you can use the built-in json module,   \  orsimplejson.  They are basically the same but sometimes simplej \ further along than the version of it that is included with \ distribution."        //"birthday": "nothing" //I comment this line     } 

This file is auto created by another program. How do I parse it with Python?

like image 906
BollMose Avatar asked Apr 30 '15 04:04

BollMose


People also ask

Can I put comments in a JSON file?

No. JSON is data-only. If you include a comment, then it must be data too. You could have a designated data element called "_comment" (or something) that should be ignored by apps that use the JSON data.

Does Python JSON support comments?

JSON doesn't permit comments by design. As explained by its creator Douglas Crockford. I removed comments from JSON because I saw people were using them to hold parsing directives, a practice which would have destroyed interoperability.

How do I parse a JSON file?

Use the JavaScript function JSON. parse() to convert text into a JavaScript object: const obj = JSON.


2 Answers

jsoncomment is good, but inline comment is not supported.

Check out jstyleson, which support

  • inline comment
  • single-line comment
  • multi-line comment
  • trailing comma.

Comments are NOT preserved. jstyleson first removes all comments and trailing commas, then uses the standard json module. It seems like function arguments are forwarded and work as expected. It also exposes dispose to return the cleaned string contents without parsing.

Example

Install

pip install jstyleson

Usage

import jstyleson result_dict = jstyleson.loads(invalid_json_str) # OK jstyleson.dumps(result_dict) 
like image 75
Jackson Lin Avatar answered Sep 17 '22 04:09

Jackson Lin


I recommend everyone switch to a JSON5 library instead. JSON5 is JSON with JavaScript features/support. It's the most popular JSON language extension in the world. It has comments, support for trailing commas in objects/arrays, support for single-quoted keys/strings, support for unquoted object keys, etc. And there's proper parser libraries with deep test suites and everything working perfectly.

There are two different, high-quality Python implementations:

  • https://github.com/dpranke/pyjson5 (written entirely in Python, it's slow, has its own test suite, project started in 2015 and more "liked"). PyPi Page: https://pypi.org/project/json5/

  • Recommended: https://github.com/Kijewski/pyjson5 (uses compiled native code via Cython which is much faster, uses the official json5 js test suite instead of its own, project started in 2018). PyPi Page: https://pypi.org/project/pyjson5/

Here's the JSON5 spec: https://json5.org/

like image 37
Mitch McMabers Avatar answered Sep 19 '22 04:09

Mitch McMabers