嵌套SON的列式CSV输出

时间:2020-03-23 18:12:54

标签: json jq

[
  {
    "name": "Metadata:MER-2.0-ver AGYW_PREV-Results (Semi Annual)",
    "id": "XOPEXepA7zg",
    "categoryOptions.name": [
      "0 -2 month",
      ">2months-<1 year",
      "< 1 year",
      "(1 - 4) Years",
      "(1-9) Years"
    ],
    "categoryOptions.id": [
      "wfvXckoyaE9",
      "Yi2K2FUDa3B",
      "kKt6hryCX75",
      "A0B8w6HoZvV",
      "upbvx1IvICR"
    ]
  },
  {
    "name": "Metadata:MER-2.0-ver KP-Results (Semi Annual)",
    "id": "k9p3Ghbi6eW",
    "categoryOptions.name": [
      "Sex Workers",
      "People in prisons and other enclosed settings (Incarcerated Population) ",
      "PWID..",
      "MSM",
      "Transgender"
    ],
    "categoryOptions.id": [
      "mwTwhESK21T",
      "eQjIwsDqbPy",
      "zYaPQA3uTiH",
      "vu0dG7psM5W",
      "Jyo9XWumVtZ"
    ]
  },
  {
    "name": "Metadata:MER-2.0-ver PP-Results (Semi Annual)",
    "id": "rkExsSSc3yI",
    "categoryOptions.name": [
      "Adolescents (10-24)",
      "Clients of  Sex Workers",
      "Displaced Persons",
      "Fishing communities",
      "Military and other Uniform Services"
    ],
    "categoryOptions.id": [
      "yWwp6xnt0pw",
      "jlKwW6DC023",
      "wF42hb47Z7J",
      "qkIUghy30Vl",
      "Vcuw6LkdAkk"
    ]
  },
  {
    "name": "Metadata:MER-2.0-ver PREP_CURR-and-TX_ML (Semi Annual)",
    "id": "ZYdO3FqQgo1",
    "categoryOptions.name": [
      "Adolescents (10-24)",
      "Clients of  Sex Workers",
      "Displaced Persons",
      "Fishing communities",
      "Military and other Uniform Services"
    ],
    "categoryOptions.id": [
      "yWwp6xnt0pw",
      "jlKwW6DC023",
      "wF42hb47Z7J",
      "qkIUghy30Vl",
      "Vcuw6LkdAkk"
    ]
  },
  {
    "name": "Metadata:MER-2.0-ver SupplyChain-Results (Semi Annual)",
    "id": "Cub0DEVWs3P",
    "categoryOptions.name": [
      "TLD 30-count bottles",
      "TLD 90-count bottles",
      "TLD 180-count bottles",
      "TLE/400 30-count bottles",
      "TLE/400 90-count bottles"
    ],
    "categoryOptions.id": [
      "dtmTsLvH2dk",
      "sOLj1z1XRxh",
      "SnkZTF4kThV",
      "sNnXSKiPvb5",
      "t3iPChPFIcd"
    ]
  }
]

预期输出应为csv格式,如下所示:

键,名称,ID,“ categoryOptions.name”,“ categoryOptions.id”

0,Metadata:MER-2.0-ver AGYW_PREV-Results (Semi Annual),XOPEXepA7zg,0 -2 month,wfvXckoyaE9

0,Metadata:MER-2.0-ver AGYW_PREV-Results (Semi Annual),XOPEXepA7zg,>2months-<1 year,Yi2K2FUDa3B

1,Metadata:MER-2.0-ver KP-Results (Semi Annual),k9p3Ghbi6eW,Sex Workers,mwTwhESK21T

1,Metadata:MER-2.0-ver KP-Results (Semi Annual),k9p3Ghbi6eWPeople in prisons and other enclosed settings (Incarcerated Population),eQjIwsDqbPy

2,元数据:MER-2.0-ver PP结果(半年度),rkExsSSc3yI,青少年(10-24岁),yWwp6xnt0pw

2,元数据:MER-2.0-ver PP结果(半年度),rkExsSSc3yI,性工作者的客户,jlKwW6DC023

最多key4

上面的输入json来自以下位置:

cat /home/fred/Downloads/metadata/multiple-dataset-metadata.json
| jq '[.dataSets[]
| {name: .name,id: .id,"categoryOptions.name": [.dataSetElements[].dataElement.categoryCombo.categories[].categoryOptions
[].name],"categoryOptions.id": [.dataSetElements[].dataElement.categoryCombo.categories[].categoryOptions[].id]}]'

2 个答案:

答案 0 :(得分:1)

据我了解,这是解决该问题的一种方法:

error: use of deleted function ‘Fork::Fork(Fork&&)’, 

...

note: ‘Fork::Fork(Fork&&)’ is implicitly deleted because the default definition would be ill-formed:

这将产生除标题行以外的所有内容,而标题行的产生将作为练习。

调用

...将遵循以下原则:

range(0;length) as $i
| .[$i]
| [$i, .name, .id] +
  ( range(0, .["categoryOptions.name"]|length) as $j
    | [ .["categoryOptions.name"][$j], .["categoryOptions.id"][$j] ] )
| @csv

答案 1 :(得分:0)

要添加到@ peak的解决方案中

最终调用(带有CSV标头)可能如下所示:

jq -r -f program.jq input.json > output.csv && sed  -i '1i "key","name","id","categoryOptions.name","categoryOptions.id"' output.csv

sed解决方案选自here