Skip to content
Advertisement

Python subprocess – git log wrong JSON Format

I tried to format git log to json but failed miserabely.

I used this command for the formatting, and I don’t think this is where my problem lies, but hey you never know.

These are my functions.

JavaScript
JavaScript

As a result I get this file – in the wrong JSON Format. Why is this and what is wrong. output/test.json

JavaScript

What do I have to change to make this a valid JSON document, which json.loads() can process.

Advertisement

Answer

Looks like you are manipulating a git log‘s output, making it a JSON file, then you’ll transfer it to some other JSON parser, and found an error there?

Yes, your output is not a valid JSON: As an “array”, a bracket wrapping the beginning and end are expected.

See https://stackoverflow.com/a/4600561/9035237https://gist.github.com/textarcana/1306223 for a post-processing example. All the code in your mentioned link said this too.

If you are using Python, you may:

JavaScript

However, there are still problems in your format: JSON doesn’t accept a line separator inside string, and a " in any field will break the format forever, but these will probably happen in a commit message. So your format should change.

As per https://gist.github.com/varemenos/e95c2e098e657c7688fd?permalink_comment_id=3260906#gistcomment-3260906 says, you can do a hack: use some string that will probably not occur in any field, for example ^^^^, as a temporary quote placeholder, then do any character escaping, for example n\n and "\", and ^^^^" at last. Don’t do JSON prettify at this step, hand it up to a JSON formatter.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement