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.

Mat Ford
  • 73
  • 1
  • 8

1 Answers1

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"
      }
    ]
  }
]
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105