JOLT 转换修改覆盖 - 替换数组中的元素值

发布于 2025-01-12 20:40:04 字数 1193 浏览 3 评论 0原文

我正在开发 JOLT 库来对 json 值进行更改。

对于键值项,我找到了一个解决方案,使用

"operation": "modify-overwrite-beta"

但是当涉及到编辑数组内的值时,我遇到了问题。

让我们以这个 JSON: SPEC 为例,

{
  "parentModule": [
    {
      "childModule": {
        "arrayModule": [
          "KK",
          "VV"
        ]
      }
    }
  ]
}

我正在使用

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "parentModule": {
        "*": {
          "childModule": {
            "arrayModule": [
              "TT",
              "RR"
            ]
          }
        }
      }
    }
  }
]

我想要的结果是该数组完全被覆盖,但目前它仅替换第一个值。

预期结果:

{
 "parentModule": [
    {
      "childModule": {
        "arrayModule": [
          "TT",
          "RR"
        ]
      }
    }
  ]
}

有没有办法:

  1. 完全覆盖数组?
  2. 有条件地更改值,例如如果 TT =>更改为 AB,否则如果 RR 则写成 BB

谢谢

I am working on JOLT library to perform a change to the json values.

For key-value items I found a solution using

"operation": "modify-overwrite-beta"

But when it comes to edit values inside the arrays I encounter problems.

Let's have for example this JSON:

{
  "parentModule": [
    {
      "childModule": {
        "arrayModule": [
          "KK",
          "VV"
        ]
      }
    }
  ]
}

SPEC I am using

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "parentModule": {
        "*": {
          "childModule": {
            "arrayModule": [
              "TT",
              "RR"
            ]
          }
        }
      }
    }
  }
]

The result I want is that the array is totally override , but currently it is replacing only the first value.

Result expected:

{
 "parentModule": [
    {
      "childModule": {
        "arrayModule": [
          "TT",
          "RR"
        ]
      }
    }
  ]
}

Is there any way to:

  1. completely override the array?
  2. change values conditionally, for example if TT => change to AB, else if RR than write BB ?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

野却迷人 2025-01-19 20:40:04

您可以将 shift 转换与 # 运算符一起使用,以表示要创建的新列表的固定元素值。

对于第一种情况(如果我们有"arrayModule": ["KK", "VV"]输入 ) :

 [
   {
     "operation": "shift",
     "spec": {
       "parentModule": {
         "*": {
           "childModule": {
             "arrayModule": {
               "#TT": "&4[&3].&2.&1[]",
               "#RR": "&4[&3].&2.&1[]"
             }
           }
         }
       }
     }
   }
]

demo1 :

在此处输入图像描述

第二次(如果我们有 "arrayModule": ["TT", "RR"] 作为输入 ) :

 [
   {
     "operation": "shift",
     "spec": {
       "parentModule": {
         "*": {
           "childModule": {
             "arrayModule": {
               "*": {
                 "TT": { "#AB": "&6[&5].&4.&3" },
                 "RR": { "#BB": "&6[&5].&4.&3" }
               }
             }
           }
         }
       }
     }
   }
]

demo2 :

在此处输入图像描述

同时设置适当的与号级别以分别在多个级别达到所需的键名称。

You can use shift transformations along with # operators in order to represent the fixed element values for the new lists to be created.

For the first case( if we have "arrayModule": ["KK", "VV"] for the input ) :

 [
   {
     "operation": "shift",
     "spec": {
       "parentModule": {
         "*": {
           "childModule": {
             "arrayModule": {
               "#TT": "&4[&3].&2.&1[]",
               "#RR": "&4[&3].&2.&1[]"
             }
           }
         }
       }
     }
   }
]

the demo1 :

enter image description here

And for the second ( if we have "arrayModule": ["TT", "RR"] for the input ) :

 [
   {
     "operation": "shift",
     "spec": {
       "parentModule": {
         "*": {
           "childModule": {
             "arrayModule": {
               "*": {
                 "TT": { "#AB": "&6[&5].&4.&3" },
                 "RR": { "#BB": "&6[&5].&4.&3" }
               }
             }
           }
         }
       }
     }
   }
]

the demo2 :

enter image description here

while setting proper ampersand levels to reach the desired key names at several levels respectively.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文