Jq:将字段映射到 csv

Jq: map fields to csv

我需要将 json 文件映射并转换为 csv。

这是我的文件。请记住,它不是数组。每个元素都在一行中。

{
   "resourceType":"PractitionerRole",
   "id":"2261026",
   "meta":{
      "versionId":"1",
      "lastUpdated":"2021-11-08T15:13:39.318+01:00",
      "source":"#wcktDPNPRW67Ths4"
   },
   "identifier":[
      {
         "system":"urn:oid:2.16.724.4.9.20.93",
         "value":"6209"
      },
      {
         "system":"urn:oid:2.16.724.4.9.20.2",
         "value":"00042"
      },
      {
         "system":"urn:oid:2.16.724.4.9.20.90",
         "value":"UAB2"
      }
   ],
   "active":true,
   "practitioner":{
      "reference":"Practitioner/1046220"
   },
   "code":[
      {
         "coding":[
            {
               "system":"http://catsalut.gencat.cat/fhir/StructureDefinition/tipus-rol-professional",
               "code":"MG"
            }
         ]
      }
   ]
}
{
   "resourceType":"PractitionerRole",
   "id":"2261027",
   "meta":{
      "versionId":"1",
      "lastUpdated":"2021-11-08T15:13:39.140+01:00",
      "source":"#l6DwTkxasiJQmfBJ"
   },
   "identifier":[
      {
         "system":"urn:oid:2.16.724.4.9.20.93",
         "value":"6734"
      },
      {
         "system":"urn:oid:2.16.724.4.9.20.2",
         "value":"01785"
      },
      {
         "system":"urn:oid:2.16.724.4.9.20.91",
         "value":"IN013"
      }
   ],
   "active":true,
   "practitioner":{
      "reference":"Practitioner/1045804"
   },
   "code":[
      {
         "coding":[
            {
               "system":"http://catsalut.gencat.cat/fhir/StructureDefinition/tipus-rol-professional",
               "code":"INF"
            }
         ]
      }
   ]
}

我需要获取这些字段:

.id, .meta.lastUpdated, .identifier[0].value, .identifier[1].value, .identifier[2].value, .code[0].coding[0].code

并将它们提取为 csv 格式的 id, lastUpdated, identifier1, identifier2, identifier3, code

有什么想法吗?

可以构造一个数组,然后直接使用@csv

$ jq -r '[.id,.meta.lastUpdated,.identifier[0].value,.identifier[1].value,.identifier[2].value,.code[0].coding[0].code] | @csv' file.txt
"2261026","2021-11-08T15:13:39.318+01:00","6209","00042","UAB2","MG"
"2261027","2021-11-08T15:13:39.140+01:00","6734","01785","IN013","INF"