2

I have a JSON file with two arrays. I'd like to combine the arrays into a single array of objects, each of which contains an element from each of the original arrays.

JSON input looks like this:

{ "en": [ "E1", "E2", "E3", "E4" ], "fr": [ "F1", "F2", "F3", "F4" ] } 

Desired output:

[ { "en":"E1", "fr":"F1"}, { "en":"E2", "fr":"F2"}, { "en":"E3", "fr":"F3"}, { "en":"E4", "fr":"F4"} ] 

No matter what I try I can't tame jq into delivering this. The closest I have come is using the filter . | {en:.en[],fr:.fr[]} which gives me every possible pairing of the arrays as follows:

{ "en": "E1", "fr": "F1" } { "en": "E1", "fr": "F2" } { "en": "E1", "fr": "F3" } { "en": "E1", "fr": "F4" } { "en": "E2", "fr": "F1" } { "en": "E2", "fr": "F2" } { "en": "E2", "fr": "F3" } { "en": "E2", "fr": "F4" } { "en": "E3", "fr": "F1" } { "en": "E3", "fr": "F2" } { "en": "E3", "fr": "F3" } { "en": "E3", "fr": "F4" } { "en": "E4", "fr": "F1" } { "en": "E4", "fr": "F2" } { "en": "E4", "fr": "F3" } { "en": "E4", "fr": "F4" } 

How can I get it to pair the first elements of each array, the second elements, etc.

2
  • Are .en and .fr always fixed? or keys name could be dynamic? Commented Oct 5, 2020 at 14:45
  • @Inian keys will change but I don't mind having a jq filter which is 'hardcoded' Commented Oct 6, 2020 at 15:33

3 Answers 3

6

You could also use transpose to "zip" the arrays together:

[[.en,.fr] | transpose[] | {en:.[0], fr:.[1]} ] 
Sign up to request clarification or add additional context in comments.

1 Comment

Nice ... thanks @peak, hadn't come across transpose before - does the job
2

I used range() to iterate over the elements of the arrays:

jq '[range(.en|length) as $i | {en: .en[$i], fr: .fr[$i]}]' file.json 

Comments

1

Here's a solution that will work with arbitrarily many keys and that does not require knowledge of the key names beforehand:

def objectify($keys): [$keys, .] | transpose | map({(.[0]): .[1]}) | add; keys_unsorted as $keys | [[.[]] | transpose[] | objectify($keys)] 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.