google / jsonnet

Jsonnet - The data templating language
http://jsonnet.org
Apache License 2.0
6.87k stars 436 forks source link

Can't parse object #1126

Closed kopyl closed 5 months ago

kopyl commented 6 months ago

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.
johnbartholomew commented 6 months ago

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 6 months ago

@johnbartholomew thank you very much, this works :)

johnbartholomew commented 5 months ago

Thanks. Closing as resolved.