0

In a directory i have few hundred json files. Each file has a json property called qlist that i want to remove from that file.

$files = Get-ChildItem "...path...\*.json"
foreach($file in $files)
{
    .\jq.exe 'del(.qlist)' $file | Out-File -FilePath $file
}

the jq part works fine, but i cannot figure out how to write output of jq into a file, code above prunes all json files for me.

For example if i would be on a llinux os would simply do

./jq 'del(.qlist)' $file > $file
mklement0
  • 382,024
  • 64
  • 607
  • 775
uiuaa
  • 23
  • 5
  • 1
    reading and writing a file in the same pipeline will result into a blank file, you need to first read, close the stream and then write to it. so, essentially, use the grouping operator `(...)`: `(.\jq.exe 'del(.qlist)' $file) | Set-Content $file` – Santiago Squarzon Feb 16 '23 at 21:31
  • As an aside: The Linux command you're showing would result in the very same problem: `$file` would get _truncated_ (reset to a zero-byte file) _before_ `jq` gets invoked. – mklement0 Feb 16 '23 at 22:54

0 Answers0