【问题标题】:JOLT shift transformation: collect the items from all the levels and change the property nameJOLT shift转换:收集所有级别的项目并更改属性名称
【发布时间】:2022-11-02 04:22:55
【问题描述】:

我正在尝试使用 Jolt 转换来转换 JSON,在此处寻找一些输入。 我正在尝试将部分项目放入一个数组中。我的目标是获得一个包含部分项目的数组。

这是我的输入和预期输出:

输入

{
  "item": [
    {
      "ac": "i",
      "id": "c5b9e16076fe4faaaace5c7d0cbe0d9b",
      "foo": {
        "id": "c5b9e16076fe4faaaace5c7d0cbe0d9b",
        "nn": "de38c8a2e6a844d1a9dd7a573eedee15"
      },
      "item": [
        {
          "ac": "y",
          "id": "c5b9e16076fe4faaaace5c7d0cbe0d9b",
          "foo": {
            "id": "c5b9e16076fe4faaaace5c7d0cbe0d9b",
            "nn": "b1754500dde646f1af495814fcd2d65e"
          }
        },
        {
          "id": "r",
          "ac": "ac",
          "foo": {
            "id": "c5b9e16076fe4faaaace5c7d0cbe0d9b",
            "nn": "bfb5550a6d754892b313ab9dd8604725"
          }
        }
      ]
    }
  ]
}

预期产出

[
  {
    "ac": "y",
    "foo": {
      "nn": [
        "b1754500dde646f1af495814fcd2d65e"
      ]
    }
  },
  {
    "ac": "i",
    "foo": {
      "nn": "de38c8a2e6a844d1a9dd7a573eedee15"
    }
  },
  {
    "ac": "r",
    "foo": {
      "nn": "bfb5550a6d754892b313ab9dd8604725"
    }
  }
]

我的规格

[
  {
    "operation": "shift",
    "spec": {
      "item": {
        "*": {
          "item": {
            "*": {
              "item": {
                "*": {
                  "ac": "[&1].ac",
                  "foo": {
                    "nn": "[&2].foo.&"
                  }
                }
              },
              "ac": "[&1].ac",
              "foo": {
                "nn": "[&2].foo.&"
              }
            }
          },
          "ac": "[&1].ac",
          "foo": {
            "nn": "[&2].foo.&"
          }
        }
      }
    }
  }
]

结果出乎我的意料,我得到了:我的输出

[ {
  "ac" : [ "y", "i" ],
  "foo" : {
    "nn" : [ "b1754500dde646f1af495814fcd2d65e", "de38c8a2e6a844d1a9dd7a573eedee15" ]
  }
}, {
  "ac" : "ac",
  "foo" : {
    "nn" : "bfb5550a6d754892b313ab9dd8604725"
  }
} ]

ac 列表和 KK 列表不是 item 的每个属性一起在一个 item 列表中

你能告诉我怎么做吗?

【问题讨论】:

    标签: java json tree jolt


    【解决方案1】:

    确定有效的分离模式以构造独立对象很重要;这种情况下我使用了"ac": "@(1,id).&1.&""nn": "@(3,id).&3.foo.KK" (作为内部的两个层次) 每个构造模型的模式,例如

    [
      {
        "operation": "shift",
        "spec": {
          "item": {
            "*": {
              "item": {
                "*": {
                  "item": {
                    "*": {
                      "ac": "@(1,id).&1.&",
                      "foo": {
                        "*": {
                          "nn": "@(3,id).&3.foo.KK"
                        }
                      }
                    }
                  },
                  "ac": "@(1,id).&1.&",
                  "foo": {
                    "*": {
                      "nn": "@(3,id).&3.foo.KK"
                    }
                  }
                }
              },
              "ac": "@(1,id).&1.&",
              "foo": {
                "*": {
                  "nn": "@(3,id).&3.foo.KK"
                }
              }
            }
          }
        }
      },
      {
       // get rid of separator labels of objects
        "operation": "shift",
        "spec": {
          "*": {
            "*": ""
          }
        }
      }
    ]
    

    【讨论】:

    • 谢谢,但是哎呀,我只是稍微改变了Q
    • @Michal 除了冗长且令人困惑的字母数字字符串值(我希望它更简单)之外,我看不到 acnn 的值之间的匹配,它们显示为预期结果的值。
    • 对不起,非常感谢,我在我的真实输入上尝试了你的规范,我只得到了“ac”而不是 foo 的值,也许是因为在我的真实输入中,除了“id”之外,我在 foo 下还有另一个属性和“nn”,你认为它相关吗?
    • 是的,这是一个错字,因为我将我的真正问题更改为一个小而简单的(相对)如果我在 foo 中有其他属性,我应该更改什么?
    • 我正在修Q
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-29
    • 1970-01-01
    • 2020-07-06
    • 1970-01-01
    • 2020-09-01
    相关资源
    最近更新 更多