2

I have a settings.json file full with useful comments (sometimes C-style and sometimes Python), and I'm programmatically modifying them with e.g. json library, but when I save the modified one I lose all the comments explaining the fields. Another inconvenience is losing the indentations and spacing therein.

Is there a 'neat' way of modifying the file programmatically?

8
  • json files could not possibly have comments and still be compliant json. so, it is not possible Commented Jan 1, 2022 at 0:03
  • Certainly not with the built-in json module, it (like JSON itself) doesn't support comments. It can pretty-print to the file though, so it's unclear what problem you've had there. Commented Jan 1, 2022 at 0:03
  • @jonrsharpe So, you are saying it will have to be some messy text parsing solution? Commented Jan 1, 2022 at 0:05
  • Well maybe, or find a Python JSON library that does support comments. Commented Jan 1, 2022 at 0:08
  • @jonrsharpe, it is not possible to find json library which can support comments. if you permit C style comments, that is not json anymore, it is something else (json5, yaml, etc) Commented Jan 1, 2022 at 0:10

1 Answer 1

3

Standard json files cannot possibly have comments and still be compliant json.

There is another format that was designed to overcome this problem: json5. It has libraries designed to keep json5 properties like comments intact - you can python library for it here.

Another approach is to keep using standard JSON but add "doc" fields for each JSON block in question. In this case, doc field(s) become data payload and will survive any transformation. For example, Apache Avro is using doc fields to document avro schema.

Sign up to request clarification or add additional context in comments.

3 Comments

It parses jsons with C-style comments seamlessly, but the comments are gone. I'm starting to realize it is not possible to have them in json format.
One solution to have comments in json that I have seen often used is to add "doc" fields for each block in question. In this case, doc field becomes data payload and will survive any transformation. For example, Apache Avro is using doc fields for their schemas
Neat! I'll follow this, but I'll have to coerce existing files to this format.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.