0

I have a JSON file like this:

{
    "download": [0, 0, 0, 0],
    "files": [ "file1.txt", "file1.iso", "file2.txt", "file2.iso" ] 
}

For each element in files[] ending in .txt I want to set the corresponding element in download[] to 1. How do I do that in JQ? I googled around but don't really understand JQ syntax.

0

1 Answer 1

2

In this case constructing download from scratch would be much easier.

.download = (.files | map(if endswith(".txt") then 1 else 0 end))

Online demo

But the title asks how to update one array based on another?, and here is one way of doing it:

.download = reduce (.files | path(.[] | select(endswith(".txt")))) as $p (.download; setpath($p; 1))

Online demo

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, actually I figured out the first solution with some trial and error but the second one is super useful!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.