Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I handle reading a .json file in it that has comments with python?

Tags:

python

json

Firstly, I understand that comments aren't valid json. That said, for some reason this .json file I have to process has comments at the start of lines and at the end of lines.

How can i handle this in python and basically load the .json file but ignore the comments so that I can process it? I am currently doing the following:

with open('/home/sam/Lean/Launcher/bin/Debug/config.json', 'r') as f:
        config_data=json.load(f)

But this crashes at the json.load(f) command because the file f has comments in it.

I thought this would be a common problem but I can't find much online RE how to handle it in python. Someone suggested commentjson but that makes my script crash saying

ImportError: cannot import name 'dump'

When I import commentjson

Thoughts?

Edit: Here is a snippet of the json file i must process.

{
  // this configuration file works by first loading all top-level
  // configuration items and then will load the specified environment
  // on top, this provides a layering affect. environment names can be
  // anything, and just require definition in this file. There's
  // two predefined environments, 'backtesting' and 'live', feel free
  // to add more!

  "environment": "backtesting",// "live-paper", "backtesting", "live-interactive", "live-interactive-iqfeed"

  // algorithm class selector
  "algorithm-type-name": "BasicTemplateAlgorithm",

  // Algorithm language selector - options CSharp, FSharp, VisualBasic, Python, Java
  "algorithm-language": "CSharp"
}
like image 698
sometimesiwritecode Avatar asked Sep 20 '17 09:09

sometimesiwritecode


People also ask

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 ignore comments in JSON?

To do this, you need to add an element to your JSON file, such as "_comment," which will contain your comment. The JSON API endpoint must ignore this particular JSON comment element.

Can JSON files have comments?

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.

How do I analyze a JSON file in Python?

Python has a built in module that allows you to work with JSON data. At the top of your file, you will need to import the json module. If you need to parse a JSON string that returns a dictionary, then you can use the json. loads() method.


2 Answers

Switch into json5. The JSON 5 is a very small superset of JSON that supports comments and few other features you could just ignore.

import json5 as json
# and the rest is the same

It is beta, and it is slower, but if you just need to read some short configuration once when starting the program, this probably can be considered as an option. It is better to switch into another standard than not to follow any.

like image 142
Audrius Meškauskas Avatar answered Oct 18 '22 07:10

Audrius Meškauskas


kind of a hack (because if there are // within the json data then it will fail) but simple enough for most cases:

import json,re

s = """{
  // this configuration file works by first loading all top-level
  // configuration items and then will load the specified environment
  // on top, this provides a layering affect. environment names can be
  // anything, and just require definition in this file. There's
  // two predefined environments, 'backtesting' and 'live', feel free
  // to add more!

  "environment": "backtesting",// "live-paper", "backtesting", "live-interactive", "live-interactive-iqfeed"

  // algorithm class selector
  "algorithm-type-name": "BasicTemplateAlgorithm",

  // Algorithm language selector - options CSharp, FSharp, VisualBasic, Python, Java
  "algorithm-language": "CSharp"
}
"""

result = json.loads(re.sub("//.*","",s,flags=re.MULTILINE))

print(result)

gives:

{'environment': 'backtesting', 'algorithm-type-name': 'BasicTemplateAlgorithm', 'algorithm-language': 'CSharp'}

apply regular expression to all the lines, removing double slashes and all that follows.

Maybe a state machine parsing the line would be better to make sure the // aren't in quotes, but that's slightly more complex (but doable)

like image 20
Jean-François Fabre Avatar answered Oct 18 '22 07:10

Jean-François Fabre