I have multiple JSON-files on a server, which can only run batch-scripts or command-line tools. The JSON files all aren't formatted properly, meaning there are no tab-spaces at the beginning of lines.
Is there a way, to write a batch-script or run a command-line tool to format the files, so they are normal again?
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 .