google/jsonnet

Can't parse object

Closed this issue · 3 comments

kopyl commented

This library can't decode JSON objects which have escaped double quotes.

results_json = """{
    title: "\"Picnic collective\" logo and characters",
}"""

results = _jsonnet.evaluate_snippet('snippet', results_json)

And you get this error:

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
Cell In[97], [line 5](vscode-notebook-cell:?execution_count=97&line=5)
      [1](vscode-notebook-cell:?execution_count=97&line=1) results_json = """{
      [2](vscode-notebook-cell:?execution_count=97&line=2)     title: "\"Picnic collective\" logo and characters",
      [3](vscode-notebook-cell:?execution_count=97&line=3) }"""
----> [5](vscode-notebook-cell:?execution_count=97&line=5) results = _jsonnet.evaluate_snippet('snippet', results_json)
      [6](vscode-notebook-cell:?execution_count=97&line=6) # print(results)

RuntimeError: STATIC ERROR: snippet:2:14-20: expected a comma before next field.

Multiline strings (triple-quoted strings) in Python still process escape sequences; they are not raw strings. So the escaped double quote of your input is being unescaped by Python when you define the results_json value, before jsonnet even sees it.

You can use a raw string (prefixed by r) instead to prevent this and get the expected result:

results_json = r"""{
    title: "\"Picnic collective\" logo and characters",
}"""

results = _jsonnet.evaluate_snippet('snippet', results_json)
kopyl commented

@johnbartholomew thank you very much, this works :)

Thanks. Closing as resolved.