{ 'abc': { 'name': 'John', 'address': 'USA' }, 'xyz': { 'name': 'Robert', 'address': 'Canada' } }
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 even tr
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"
}
}