如何使用jq创建转换后的嵌套JSON元素?

时间:2019-07-10 09:13:52

标签: json jq

我有一个带有嵌套元素的JSON文件,我试图将其处理成一个未嵌套的JSON文件。我该怎么做?

使用js,我试图隔离这些百分比,这是我能够做到的。我不确定如何重命名百分比字段。我见过一些实例,看起来value.gender或value.grade应该可以工作,但是我也不知道如何组合它。


jq '.data[] | .id as $id | (.demographics[] | .percentage as $percentage | .gender as $gender | .grade as $grade | {"id":$id, "percentage":$percentage})' test2.json

在这里,我希望能够将百分比字段重命名为性别和等级值。然后,我想按ID分组。

这是原始的JSON文件(test2.json):


{
   "data": [{
         "id": "abc",
         "students": "elementary",
         "demographics": [{
               "grade": "K-2",
               "percentage": "0.1",
               "gender": "unspecified"
            },
            {
               "grade": "K-2",
               "gender": "male",
               "percentage": "0.5"
            },
            {
               "gender": "female",
               "percentage": "0.4",
               "grade": "K-2"
            },
            {
               "grade": "3-6",
               "percentage": "0.3",
               "gender": "male"
            },
            {
               "percentage": "0.2",
               "gender": "unspecified",
               "grade": "3-6"
            },
            {
               "grade": "3-6",
               "gender": "female",
               "percentage": "0.5"
            }
         ],

         "neighborhood_name": [{
               "percentage": "0.5",
               "neighborhood": "atwood"
            },
            {
               "region": "bluff",
               "percentage": "0.5"
            }
         ]
      },
      {
         "id": "def",
         "students": "midhigh",
         "demographics": [{
               "grade": "7-9",
               "percentage": "0.2",
               "gender": "unspecified"
            },
            {
               "grade": "7-9",
               "gender": "male",
               "percentage": "0.2"
            },
            {
               "gender": "female",
               "percentage": "0.6",
               "grade": "7-9"
            },
            {
               "grade": "10-12",
               "percentage": "0.1",
               "gender": "male"
            },
            {
               "percentage": "0.1",
               "gender": "unspecified",
               "grade": "10-12"
            },
            {
               "grade": "10-12",
               "gender": "female",
               "percentage": "0.8"
            }
         ],

         "neighborhood_name": [{
               "percentage": "0.2",
               "neighborhood": "atwood"
            },
            {
               "region": "bluff",
               "percentage": "0.8"
            }
         ]

      }
   ]
}

这是我的期望:


{
         "id": "abc",
         "students": "elementary",
         "demo_K-2_unspecified": "0.1",
         "demo_K-2_male": "0.5",
         "demo_K-2_female": "0.4",
         "demo_3-6_male": "0.3",
         "demo_3-6_unspecified": "0.6",
         "demo_3-6_female": "0.5",
            },
      {
         "id": "def",
         "students": "midhigh",
         "demo_7-9_unspecified": "0.2",
         "demo_7-9_male": "0.2",
         "demo_7-9_female": "0.6",
         "demo_10-12_male": "0.1",
         "demo_10-12_unspecified": "0.1",
         "demo_10-12_female": "0.8",

      }

2 个答案:

答案 0 :(得分:1)

使用示例数据,以下过滤器将产生所需的输出:

val intLiveData = Transformations.map(text) {
        try {
            it.toInt()
        } catch (e: NumberFormatException) {
            0
        }
    }

这里的主要思想是使用.data[] | {id, students} as $ix | .demographics | map( {"demo_\(.grade)_\(.gender)": .percentage} ) | $ix + add 创建键值对的列表,以便可以使用map轻松地创建复合对象。

单线

add

答案 1 :(得分:0)

或者,可以使用unix步行路径实用程序 jtc 实现相同的JSON操作:

bash $ <test2.json jtc -x'[data][:]' -y[id] -y[students] -y'<grade>l:<R>v[-1][gender]<N>v[-1][percentage]' -TT -TT -T'{"demo_{R}_{N}":{{}}}' -jll
[
   {
      "demo_3-6_female": "0.5",
      "demo_3-6_male": "0.3",
      "demo_3-6_unspecified": "0.2",
      "demo_K-2_female": "0.4",
      "demo_K-2_male": "0.5",
      "demo_K-2_unspecified": "0.1",
      "id": "abc",
      "students": "elementary"
   },
   {
      "demo_10-12_female": "0.8",
      "demo_10-12_male": "0.1",
      "demo_10-12_unspecified": "0.1",
      "demo_7-9_female": "0.6",
      "demo_7-9_male": "0.2",
      "demo_7-9_unspecified": "0.2",
      "id": "def",
      "students": "midhigh"
   }
]
bash $ 

这里有3条步行路径(将-x与每个-y连接在一起之后):

  • 第一条步行路径([data][:][id])将显示id条记录,第二条将进行students,最后一条将收集所有demographics条记录
  • 有3个模板(每个走步一个),因为前两个走步不需要任何插值,因此它们的模板是虚拟的(-TT)。最后一个模板将最后的步行转换为所需的输出。

选项-j将所有遍历收集到外部JSON数组中; -ll确保模板化的JSON对象将被剥离(即视为标记的值)并通过漫游相关性分组在一起。

PS>披露:我是jtc-用于JSON操作的shell cli工具的创建者