skip to Main Content

I have json like this:

[
    {
        "id": "1"
    }, 
    {
        "id": "2"
    }, 
    {
        "id": "3"
    } 
]

I call some "sizes.sh" that give me result:

10
20
30

How can I update my original json with these results, so it will look:

[
    {
        "id": "1",
        "size": 10
    }, 
    {
        "id": "2",
        "size": 20

    }, 
    {
        "id": "3",
        "size": 30

    }
]

2

Answers


  1. Here is a way:

    ./sizes.sh | jq '[., [{size: inputs}]] | transpose | map(add)' file.json -
    
    Login or Signup to reply.
  2. How can I update my original json

    jq cannot "update" a file, you’ll have to let the shell do it. Assuming a POSIX shell, you could redirect the output to a temporary file using >, and overwrite the original on success using mv -f:

    ./sizes.sh |
    jq -Rn --slurpfile in original.json > temp.json 
      '$in + [[{size: inputs}]] | transpose | map(add)' &&
    mv -f temp.json original.json
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search