首页 > 解决方案 > 需要 Jolt Spec 将矩阵 json 转换为非规范化 json 格式

问题描述

谁能帮我一个JOLT 规范,将我的矩阵类型 json 转换为非规范化 json。请在下面找到我的输入 json 和我预期的 josn 输出。

输入Json:

[
  {
    "attributes": [
      {
        "name": "brand",
        "value": "Patriot Lighting"
      },
      {
        "name": "color",
        "value": "Chrome"
      },
      {
        "name": "price",
        "value": "49.97 USD"
      }
    ]
  },
  {
    "attributes": [
      {
        "name": "brand",
        "value": "Masterforce"
      },
      {
        "name": "color",
        "value": "Green"
      },
      {
        "name": "price",
        "value": "99.0 USD"
      }
    ]
  }
]

预期的 Json 输出:

  [
    {
      "brand": "Patriot Lighting",
      "color": "Chrome",
      "price": "49.97 USD"
    },
    {
      "brand": "Masterforce",
      "color": "Green",
      "price": "99.0 USD"
    }
  ]

我试图构建 JOLT 规范来转换这个 json。但挑战是我有多个带有“属性”标签的表的 json。

提前致谢!

标签: jsonjoltdenormalizationjsonconverter

解决方案


JOLT 不容易使用,但我从其他一些 StackOverflow 问题中得到了很多,我刚刚开始阅读源代码注释

[
  {
    "operation": "shift",
    "spec": {
      // for each element in the array
      "*": {
        "attributes": {
          // for each element in the attribute
          "*": {
            // grab the value
            // - put it in an array
            //   - but it must be indexed by the "positions" found four steps back
            // - put the value in a key
            //   - that is determined by moving one step back and looking at member name
            "value": "[#4].@(1,name)"
          }
        }
      }
    }
  }
]

乍一看这似乎很模糊,但我希望评论能解释一切。

请阅读JOLT 转换以沿数组复制单个值

这对于 JOLT 初学者来说几乎是强制性的https://docs.google.com/presentation/d/1sAiuiFC4Lzz4-064sg1p8EQt2ev0o442MfEbvrpD1ls/edit#slide=id.g9a487080_011

如果您需要另一个示例,我刚刚在这里回答了一个问题Nifi JOLT: flat JSON object to a list of JSON object

而且,您最好的朋友可能可以在https://jolt-demo.appspot.com找到


推荐阅读