JSON: Difference between revisions

579 bytes added ,  9 years ago
jq
(jq)
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:
<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}}==
2,515

edits