根据两个条件替换扁平 JSON 中的值

问题描述 投票:0回答:1

在 jolt 的帮助下,我成功地压平了 JSON。 扁平化的 JSON 现在应该充当 INPUT JSON 对于以下情况:

每次出现条件时

"values-*-identifier" = "colour"  AND  "values-*-value" = "green" 

已满,值

"green"
应替换为
"gre"

这意味着规范必须使用“AND”执行 if/else,并且必须比较键内的索引,该索引应该具有相同的数字,以保证该值属于标识符。

输入 JSON:

{
  "values_0_identifier": "country",
  "values_0_value": "red",
  "values_0_value_unit_key": "",
  "values_0_language": "",
  "values_1_identifier": "metal",
  "values_1_value": "red",
  "values_1_value_unit_key": "",
  "values_1_language": "",
  "values_2_identifier": "colour ",
  "values_2_value": "green",
  "values_2_value_unit_key": "",
  "values_2_language": ""
}

期望的输出

{
  "values_0_identifier": "country",
  "values_0_value": "red",
  "values_0_value_unit_key": "",
  "values_0_language": "",
  "values_1_identifier": "metal",
  "values_1_value": "red",
  "values_1_value_unit_key": "",
  "values_1_language": "",
  "values_2_identifier": "colour ",
  "values_2_value": "gre",
  "values_2_value_unit_key": "",
  "values_2_language": ""
}

到目前为止我尝试过这个规格

[
  {
    "operation": "shift",
    "spec": {
      "values_2_value": {
        "green": {
          "values_2_identifier": {
            "colour": {
              "#gre": "value_2_identifier"
            }
          }
        }
      },
      "*": "&"
    }
  }
]

第一个问题:我的 if_else 条件没有结果。看起来,它正在吞噬映射的值。 第二个问题:即使这个规范应该有效。当然,我不想在键中指定值“-2-”。在这里,像 -- 这样的通配符应该可以工作。其中 -- 自然应该始终具有相同的索引。
每一种帮助都将受到高度赞赏。预先感谢您的任何提示。

json mapping wildcard jolt
1个回答
0
投票

您可以通过使用

开始按索引0,1,2分组
  • &(2,1):去2升级树并选择第一个星号替换
  • &(2,2)
  • :去2升级树并选择2第一个星号替换
  • 表示如

[ { //group by indexes 0,1,2 "operation": "shift", "spec": { "values_*_*": { "*": { "@1": "&(2,1).&(2,2)" } } } }, { // "operation": "shift", "spec": { "*": { "*": "values_&1_&", //other attributes "value": { "": "" }, //get rid of extra "value" attributes "identifier": { "@": "values_&2_&", // to replicate "identifier" itself "*": { // if identifier IS NOT colour "@1": "values_&3_value" }, "colour": { "@2,value": { "green": { "#gre": "values_&5_value" }, "*": { // value IS NOT green "@1": "values_&5_value" } } } } } } } ]

网站上的 
演示

https://jolt-demo.appspot.com/ 是:

© www.soinside.com 2019 - 2024. All rights reserved.