我正在try 编写一个规范来使用Jolt转换来完成下面的转换.我需要将平面Json转换为嵌套Json

我在将平面JSON转换为嵌套JSON时遇到了一些问题.我已经看了一些例子,但没有更深入地了解上面提到的内容.我需要使用Jolt规范来转换JSON struct .我使用https://jolt-demo.appspot.com来测试以下内容.

Input:

[
  {
    "container_id": "a",
    "carrier_scac": "b",
    "location": "banglore",
    "state": "karnataka",
    "country": "India"
  },
  {
    "container_id": "a",
    "carrier_scac": "b",
    "location": "pune",
    "state": "maharashtra",
    "country": "India"
  },
  {
    "container_id": "c",
    "carrier_scac": "d",
    "location": "dharwad",
    "state": "kan",
    "country": "India"
  },
  {
    "container_id": "c",
    "carrier_scac": "d",
    "location": "hubli",
    "state": "kant",
    "country": "India"
  }
]

Desired Output:

[
  {
    "load": {
      "container_id": "a",
      "carrier_scac": "b",
      "stops": [
        {
          "location": "banglore",
          "state": "karnataka"
        },
        {
          "location": "pune",
          "state": "maharashtra"
        }
      ]
    },
    "containerInfo": {
      "country": "India"
    }
  },
  {
    "load": {
      "container_id": "c",
      "carrier_scac": "d",
      "stops": [
        {
          "location": "dharwad",
          "state": "kan"
        },
        {
          "location": "hubli",
          "state": "kant"
        }
      ]
    },
    "containerInfo": {
      "country": "India"
    }
  }
]

我正在使用的Jolt Spec:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "container_id": "@(1,container_id).&",
        "carrier_scac": "@(1,container_id).&",
        "*": "@(1,container_id).stops[&1].&"
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": "=recursivelySquashNulls"
    }
  },
  {
    "operation": "cardinality",
    "spec": {
      "*": {
        "container_id": "ONE",
        "carrier_scac": "ONE"
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": ""
    }
  }
]

推荐答案

目前的规格相当不错,只需要一些小的修改,比如增加100101个 node ,并缩短一点,如下所示

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": "@(1,container_id).load.stops[&1].&", // "else" case
        "country": "@(1,container_id).load.containerInfo.&",
        "c*": "@(1,container_id).load.&" // the attributes starting with "c" but other than "country" 
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": "=recursivelySquashNulls"
    }
  },
  {
    "operation": "cardinality",
    "spec": {
      "*": {
        "*": {
          "c*": "ONE", 
          "containerInfo": {
            "*": "ONE"
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": ""
    }
  }
]

Json相关问答推荐

Google Page to JSON应用程序脚本出现CORS错误

Swift解码错误类型与`Bool`type不一致

筛选JSON数组以使用Jolt仅保留具有最新日期/时间的条目

织女星-没有循环的动画条形图第二部分(实际上是织女星)

如何将文件夹组织的.json文件合并为一个JSON文件,文件夹/文件名作为键

如何使用模式注册中心创建从主题中取消本地化的ks qlDB表?

如何从一个700MB的json文件中列出PowerShell中的所有密钥?

基于 JSON 字段的 Jolt 条件标志

爆炸没有数组的 struct pyspark

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

嵌套存储在字符串变量中的 JSON 对象

如果 JSON 对象包含列表中的子字符串,则丢弃它们

如何用 Xidel 正确读取这个 JSON 文件?

hook到 Decodable.init() 以获得未指定的键?

Newtonsoft Json 将值 {null} 转换为类型System.Int32时出错

如何使用 Jackson 定义可选的 json 字段

在 Android 中使用带有 post 参数的 HttpClient 和 HttpPost

Retrofit2.0 得到 MalformedJsonException 而 json 似乎正确?

JSON 格式的 Amazon S3 响应?

C++:使用 nlohmann json 从文件中读取 json 对象