Windows Batch - Pretty Print JSON per Script
You will definitely need an external tool for that. I'm using the command-line JSON processor jq
- just pass it the Identity filter .
and it will pretty print the JSON it's been given as input.
Update: Corrected syntax (where did that come from?), Sorry @Delfic and @Sonata. Even though the empty filter ""
from my original answer works, I changed the filter like @Wolf said, as it is indeed mentioned in the manual as the "absolute simplest filter".
Example:
ECHO { "att1": 1, "att2": 2 } | jq .
Output:
{
"att1": 1,
"att2": 2
}
If you want to process JSON input directly from the shell, you can also use null input -n
:
jq -n "{ "att1" : 1, "att2" : 2 }"
To process a file, do:
jq . dirty.json
...or, if you're so inclined:
TYPE dirty.json | jq .
Super quick way to do it if you have python installed is by typing following in the command prompt or powershell:
type dirty.json | python -m json.tool > pretty.json
where dirty.json is minified or unreadable json and pretty.json is pretty one. you can even put it in a batch file to run it by passing an argument. Its very fast in processing.
Explanation:
type is writing the file content to the console but its piped so it is being sent to python's module json.tool and then '>' writes the output into a file called 'pretty.json'. Try without the > pretty.json
to see the output in the console.