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.

Example JSON transformer doesn't process escape sequences

See original GitHub issue

The JSON transformer presented in the documentation doesn’t handle the escape sequences in JSON strings. For example, copy the last full source (from Part 5 - Step 1) to a lark-json script and run it:

$ python lark-json /dev/stdin
"foo\nbar"
^D
foo\nbar

The expected output would be foo and bar in two separate lines.

An easy and (I hope) fast way to achieve this is to use the unicode_escape built-in codec, which also handles the \uhhhh sequences:

diff --git a/lark-json.orig b/lark-json
index 3d07bd6..8442239 100644
--- a/lark-json.orig
+++ b/lark-json
@@ -25,7 +25,7 @@ json_grammar = r"""
 
 class TreeToJson(Transformer):
     def string(self, (s,)):
-        return s[1:-1]
+        return s[1:-1].decode('utf-8').decode('unicode_escape')
     def number(self, (n,)):
         return float(n)
 

This will also modify string to return Unicode strings in Python 2. Since JSON is defined in terms of Unicode, this is probably the right thing to do, but if it’s undesirable, the result could be re-encoded to UTF-8.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:13 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
goodmamicommented, Mar 23, 2020

Maybe something like this: http://www.json.org/JSON_checker/

See the linked test suite. Note, however, that this test suite expects the top-level element to only be an object or an array, but the spec allows other value types. I think the spec may have changed at some point and the tests were not updated, but it still is a useful set of examples.

1reaction
erezshcommented, Mar 23, 2020

To expand a little on what I mean by real attempt: Lark’s example Python parser can parse every Python file in the built-in library, and a special Python file full of gotchas, made by someone who isn’t me.

If you can get a JSON parser to that level of rigor, I will be happy to include it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to escape special characters in building a JSON string?
Try JSON.parse("'foo'") in your browser console, for example, and observe the SyntaxError: Unexpected token ' . The JSON ...
Read more >
XML JSON Transformer service - IBM
The XML JSON Transformer service converts the XML or JSON input data to the ... the angular brackets must be replaced with escape...
Read more >
Transform failed to process result using 'successTemplate'
My goal is to convert a part of the normal JSON to a variable of type string (JSON Escape), and for that I...
Read more >
Solve common issues with JSON in SQL Server
JSON generated with the WITHOUT_ARRAY_WRAPPER clause is escaped in FOR JSON output ; SELECT 'Text' as ; FOR JSON PATH ...
Read more >
JSON formatting with jq and CI/CD linting automation | GitLab
A “parse: unexpected character" error occurs when passing a value that is not a valid JSON string to the JSON. parse method, for...
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