JSON: Difference between revisions
Content added Content deleted
(jq) |
|||
Line 1,087: | Line 1,087: | ||
However, there is an ambiguity with parsing JavaScript object literals by themselves, where it might be mistakenly interpreted as a block, and the key followed by a colon as a label. To avoid this, remember to surround it in parentheses to force it to be interpreted as an expression: |
However, there is an ambiguity with parsing JavaScript object literals by themselves, where it might be mistakenly interpreted as a block, and the key followed by a colon as a label. To avoid this, remember to surround it in parentheses to force it to be interpreted as an expression: |
||
<lang javascript>var data = eval('(' + '{ "foo": 1, "bar": [10, "apples"] }' + ')');</lang> |
<lang javascript>var data = eval('(' + '{ "foo": 1, "bar": [10, "apples"] }' + ')');</lang> |
||
=={{header|jq}}== |
|||
JSON is jq's native data format, so nothing need be done to parse JSON input. For example, to "pretty print" a stream of JSON entities (including scalars), it would be sufficient to use the jq program:<lang jq> . </lang> |
|||
Here are the jq equivalents of the examples given in the section on Julia, assuming the file data.json holds the following JSON text: |
|||
{ "blue": [1,2], "ocean": "water" } |
|||
<lang jq>jq -c . data.json</lang> |
|||
produces: |
|||
{"blue":[1,2],"ocean":"water"} |
|||
<lang jq>jq tostring data.json</lang> |
|||
produces: |
|||
"{\"blue\":[1,2],\"ocean\":\"water\"}" |
|||
=={{header|Julia}}== |
=={{header|Julia}}== |