4

If I have an array of objects like this:

[
    {
        "remote" : [
            {
                "id" : 1
            },
            {
                "id" : 2
            },
            {
            "   id" : 3
            }
        ],
        "text_id" : 1
        },
    {
        "remote" : [
            {
                "id" : 4
            },
            {
                "id" : 5
            },
            {
                "id" : 6
            }
        ],
        "text_id" : 2
    }
]

How would you add "text_id" field to every object inside .[].remote[] array so it would become

[
    {
        "remote" : [
            {
                "id" : 1,
                "text_id" : 1
            },
            {
                "id" : 2,
                "text_id" : 1
            },
            {
            "   id" : 3,
                "text_id" : 1
            }
        ]
        },
    {
        "remote" : [
            {
                "id" : 4,
                "text_id" : 2
            },
            {
                "id" : 5,
                "text_id" : 2
            },
            {
                "id" : 6,
                "text_id" : 2
            }
        ]
    }
]

I have already spent several hours trying to figure this out. It looks like there has to be a way to do this using foreach directive, but after I checked the manual for it, it seemed to me pretty obscure so I though maybe someone could give an example. Thanks.

3 Answers 3

6

You don't need map for that.

.[] |= (.remote[] += {text_id} | del(.text_id))

Online demo

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

2 Comments

Thanks, your answer looks shorter, although I used jq '.[] | {remote, text_id} | .remote[] + {text_id}' json.log in the end, but it was inspired by your version=) It seems I had those freaking dots messed up when composing objects, because I was trying to do that and only got jq's error messages, which were not helpful at all.
@krop Good. .[] | .remote[] + {text_id} would do the same thing though.
2
jq 'map( .text_id as $t
         | .remote |= map( . + {text_id : $t} )
         | del(.text_id)
    )'

Comments

0

I think you should use array.map() You have two levels of array so it should look like this :

const fatArray = [
    {
        remote : [
            {
                id : 1
            },
            {
                id : 2
            },
            {
                id : 3
            }
        ],
        text_id : 1
        },
    {
        remote : [
            {
                id : 4
            },
            {
                id : 5
            },
            {
                id : 6
            }
        ],
        text_id : 2
    }
];
const finalArray = fatArray.map( arr => {
  return arr.remote.map(elem => { return {text_id: arr.text_id, id: elem.id}})
}
)
console.log(finalArray);

2 Comments

THanks but I meant this jq: stedolan.github.io/jq/manual/#example28
oops my bad sorry !

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.