0

My python program generates multiple json file as output. But those json are not formatted by default . I would like to format them and save it back using cli. Currently I am doing format document option in vscode but that i have to do one by one file.

cat file.json | jq .

The above command displays it but do not know how to save it back to the same file. Also i would like to do it for entire folder.

Samselvaprabu
  • 16,830
  • 32
  • 144
  • 230
  • 4
    jq has very limited IO capabilities, so you'd need to fall back to operating from shell (writing a shell script) to accomplish it. That said, wouldn't it be simpler to pretty-print the files directly from your python program? – pmf Aug 17 '23 at 11:00
  • 1
    @pmf Thanks for your suggestion. I am using pandas df.to_json(). The method has indent argument. When i passed indent=4 ,it shows properly formatted – Samselvaprabu Aug 17 '23 at 11:13
  • _Also i would like to do i ..._ : Please ask only one thing in one question. How to perform an operation for every file in a directory, is an independent problem of how to deal with the case that the input file for a command and its output file are supposed to be the same. – user1934428 Aug 17 '23 at 11:46
  • See [here](https://meta.stackexchange.com/questions/222735/can-i-ask-only-one-question-per-post) – user1934428 Aug 17 '23 at 11:53
  • For saving to the same file you can use command `cat file.json | jq . > file.json`. But I agree with previous comments - the best way will be use internal python tools for formatting – Ilkiv Yuriy Aug 18 '23 at 10:11
  • @IlkivYuriy no, that won't work. `cat file | something > file` will result in an empty file. Also, useless use of cat; jq can read files just fine by itself: `jq . file.json` (and even if it couldn't, shells can redirect input: `jq . < file.json`; no need to start one additional process) – knittl Aug 21 '23 at 06:15

1 Answers1

1

To modify several files through a text-filter program, you'd generally want to use a loop with POSIX shells (e.g. bash).

for file in *.json; do
  jq . < "$file" > "$file.out" && mv "$file.out" "$file"
done

Reading and writing to different file names is crucial, because the > IO redirect operator truncates the file before the application is started and you would end up with an empty file.

Also be careful with vanilla jq and numbers in your input files, because it does not handle arbitrary precision numbers and will produce "rounded" numbers (up to IEEE floating point precision); if that is a requirement, switch to one of the jq clones such as gojq.

Or use python itself to pretty-print your JSON: python3 -m json.tool "$file"

knittl
  • 246,190
  • 53
  • 318
  • 364