question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

find_json() may output malformed JSON due to naive splitlines() usage

See original GitHub issue

The find_json function in salt.utils.json is designed to extract valid JSON from a raw string which may begin with invalid JSON (e.g. due to a plain text header preceding the JSON data). It does this by calling Python’s splitlines() on the raw string and attempting to load the resulting data as JSON by incrementally removing the beginning lines until it succeeds or it runs out of lines.

Unfortunately the call to splitlines() is naive here as it will split on characters which are valid to embed in JSON but will result in malformed JSON if converted to newlines. A complete listing is container in the stdlib documentation. Particularly problematic are the splitting on U+2028 (Line Separator) and U+2029 (Paragraph Separator).

We recently encountered this issue on a production system where the output from npm audit as part of an npm.install call via the npm.bootstrap state included several U+2028 characters in one of the JSON values. Salt split these characters into newlines resulting in malformed JSON, and the state would be marked as failed due to the missing changes dictionary in the returned output.

I’m not clear what the best way to solve this is, but expect that converting those Unicode characters to newlines is probably not the correct behaviour in any scenario we’d expect to encounter?

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:1
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
ralishcommented, Aug 20, 2020

@sagetherage @waynew Sorry for the delayed response! Yes, my read of @waynew’s approach seems like a pretty elegant fix for the issue 👍

0reactions
sagetheragecommented, Aug 20, 2020

sorry, my mistake in closing, re-opened

Read more comments on GitHub >

github_iconTop Results From Across the Web

python - How do I automatically fix an invalid JSON string?
The answer by @Michael gave me an idea... not a very pretty idea, but it seems to work, at least on your example:...
Read more >
Resolve JSON errors in Amazon Athena
When I try to read JSON data in Amazon Athena, I receive NULL or incorrect data errors. Resolution. Check the following common issues:...
Read more >
Solved: Error: Parse JSON "The request content was invalid...
Solved: How can I fix the Error " The request content was invalid and ... This schema generate from JSON data by "Use...
Read more >
Handling non-compliant JSON with Jackson - cowtowncoder
less typing? Usage pattern is similar to other features: JsonFactory f = JsonFactory.builder() .enable(JsonReadFeature.ALLOW_SINGLE_QUOTES) .
Read more >
JSON.parse() - JavaScript - MDN Web Docs
parse() method parses a JSON string, constructing the JavaScript value or object described by the string. An optional reviver function can be ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found