{ 'abc': { 'name': 'John', 'address': 'USA' }, 'xyz': { 'name': 'Robert', 'address': 'Canada' } }
Asked
Active
Viewed 95 times
1 Answers
0
The sample is not valid JSON.
jq can be used to pretty-print valid JSON, though there are some important caveats, mainly about numbers. For example:
$ jq . <<< '{ "abc": { "name": "John", "address": "USA" }, "xyz": { "name": "Robert", "address": "Canada" } }' { "abc": { "name": "John", "address": "USA" }, "xyz": { "name": "Robert", "address": "Canada" } }
See the jq FAQ for information about converting not-quite-valid JSON to JSON -- search for
not-quite-valid
.At least one of the tools mentioned in the above-referenced section in the jq FAQ (jsonlint) will not only convert single-quoted quasi-JSON to JSON, but also pretty-print it.
In the example you gave, you could use
sed
or eventr
in conjunction with jq:echo "{ 'abc': { 'name': 'John', 'address': 'USA' }, 'xyz': { 'name': 'Robert', 'address': 'Canada' } }" | tr "'" '"' | jq . { "abc": { "name": "John", "address": "USA" }, "xyz": { "name": "Robert", "address": "Canada" } }

peak
- 105,803
- 17
- 152
- 177