2

Input json:

[
   {
      "Authors": "Author1, Author2, Author3",
      "Affiliation": "Here, There, Everywhere"
   },
   {
      "Authors": "Author4, Author5",
      "Affiliation": "Nirvana, Utopia"
   }
]

Desired output:

{
   "authors": [
      {
         "Name": "Author1",
         "Affiliation": "Here"
      },
      {
         "Name": "Author2",
         "Affiliation": "There"
      },
      {
         "Name": "Author3",
         "Affiliation": "Everywhere"
      }
   ]
},
{
   "authors": [
      {
         "Name": "Author4",
         "Affiliation": "Nirvana"
      },
      {
         "Name": "Author5",
         "Affiliation": "Utopia"
      }
   ]
}

I can read the first elements of both arrays with:

jq '.[] as $o | $o.Authors | split(", ") as $authors | $o.Affiliation | split(", ") as $affiliation | { "Authors": [ { "Name": $authors[.0], "Affiliation": $affiliation[.0]} ] }'

but I'm struggling to understand how to get jq to iterate over the entire (arbitrary length) string to produce the full desired output.

1 Answer 1

3

jq solution:

jq '[.[] | [(.Authors | split(", ")), (.Affiliation | split(", "))] 
         | transpose | { authors: map({ Name:.[0], Affiliation:.[1] }) }]' input.json

The output:

[
  {
    "authors": [
      {
        "Name": "Author1",
        "Affiliation": "Here"
      },
      {
        "Name": "Author2",
        "Affiliation": "There"
      },
      {
        "Name": "Author3",
        "Affiliation": "Everywhere"
      }
    ]
  },
  {
    "authors": [
      {
        "Name": "Author4",
        "Affiliation": "Nirvana"
      },
      {
        "Name": "Author5",
        "Affiliation": "Utopia"
      }
    ]
  }
]
Sign up to request clarification or add additional context in comments.

Comments

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.