简体   繁体   中英

Using jq to convert array of strings into array of objects

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.

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"
      }
    ]
  }
]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM