Search code examples
jsonoptimizationcmdfile-iojq

jq: Format a JSON file with the output replacing the input file


I want to use jq to format a JSON file in Windows. So far, I haven't been able to find a way to perform this task using a single jq command.

Thus, I wrote this batch file:

rem create formatted JSON output file
jq-win64.exe . "%1" >"temp.tmp"

rem replace the unformatted JSON input file with the formatted JSON output file
del "%1"
ren "temp.tmp" "%1"

Is there a way to instruct jq to do this without performing these gymnastics?


Solution

  • You want the following:

    jq . "%1" >"%1".tmp
    if not errorlevel 1 move /y "%1".tmp "%1" >nul
    

    jq has no option to do this for you.