我想删除所有嵌套数组,并将"Location"和"metaRecords"的值放入rest json对象中,最后删除这些对象("Location"和"metaRecords").

{
  "info": [
    {
      "item": "sugar",
      "price": 4,
      "state": "Alberta",
      "country": "Canada"
    },
    {
      "item": "sugar",
      "price": 4.5,
      "state": "California",
      "country": "US"
    }
  ],
  "location": {
    "continent": [
      {
        "country": "US",
        "coordinates": {
          "lang": "97º 00' W",
          "long": "38º 00' N"
        }
      },
      {
        "country": "Canada",
        "coordinates": {
          "lang": "95° 00' W",
          "long": "60° 00' N"
        }
      }
    ]
  },
  "metarecord": {
    "continent": "North America"
  }
}

以下是我想要实现的目标: 删除所有嵌套数组,并使用Country的值将"Location"的值放入其相应的json对象中(例如,如果对象的Country值是"Canada",则应添加"Canada"的位置),而"metaRecords"是对"info"中的所有对象的常规设置

[
  {
    "continent": "North America",
    "item": "sugar",
    "price": 4,
    "state": "Alberta",
    "country": "Canada",
    "lang": "95° 00' W",
    "long": "60° 00' N"
  },
  {
    "continent": "North America",
    "item": "sugar",
    "price": 4.5,
    "state": "California",
    "country": "US",
    "lang": "97º 00' W",
    "long": "38º 00' N"
  }
]

我try 了什么:

[
  {
    "operation": "shift",
    "spec": {
      "info": {
        "*": {
          "item": "[&1].item",
          "price": "[&1].price",
          "state": "[&1].state",
          "country": "[&1].country"
        }
      },
      "location": {
        "continent": {
          "@": "[&1].continent"
        }
      },
      "metarecord": {
        "continent": {
          "*": {
            "$": "[&2].&1"
          }
        }
      }
    }
  }
]

输出:这不是我想要的.

[
  {
    "item": "sugar",
    "price": 4,
    "state": "Alberta",
    "country": "Canada"
  },
  {
    "item": "sugar",
    "price": 4.5,
    "state": "California",
    "country": "US"
  }
]

推荐答案

您可以使用以下转换

[
  { // you can group by the countries while matching the ones taken from different arrays
    "operation": "shift",
    "spec": {
      "info": {
        "*": {
          "*": "@1,country.&",
          "@2,location.continent": { // go two levels up the tree to grab the values
            "*": {
              "coordinates": { "*": "@2,country.&" }
            }
          },
          "@2,metarecord.continent": "@1,country.continent"
        }
      }
    }
  },
  { // pick only one from the repeating components for the arrays
    "operation": "cardinality",
    "spec": {
      "*": {
        "*": "ONE"
      }
    }
  },
  { // get rid of the object labels
    "operation": "shift",
    "spec": {
      "*": ""
    }
  }
]

站点http://jolt-demo.appspot.com/上的演示是:

enter image description here

Json相关问答推荐

我可以使用JQ来缩小数组中的json对象的范围吗?

将数组中的值作为键连接到另一个数组中的值(Jolt)

在AWS步骤函数中将字符串解析为JSON&S映射状态

Vega-Lite规范:尽管在规范中提供了数据,但显示空图表

转换为Json时忽略内部对象中的数组

将=分隔值文件转换为:json文件

使用带有逗号的字段名称构建 struct

Powershell 无法从名为 count 的键中获取价值

使用 JQ 从文件中删除重复的 JSON 块

如何使用 LINQ 在 C# 中重构对象?

将 YAML 文件转换为 Python JSON 对象

如何在 jQuery 中循环遍历 JSON 数组?

Android JSON 库的性能和可用性比较

Jackson 动态属性名称

Jsonpath 与 Jackson 或 Gson

如何向(JSON)对象的原型添加方法?

如何让 javascript 从 .json 文件中读取?

有没有办法折叠 Postman 中的所有 json 字段

将 JsonArray 添加到 JsonObject

Backbone.js 模型与集合