JDT转换以修改第N个数组元素 [英] JDT transform to modify N-th array element

查看:136
本文介绍了JDT转换以修改第N个数组元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 JDT 转换为JSON文档,以便修改第N个数组元素中的属性.不必替换整个元素甚至整个数组,这是否可能?

I am trying to apply a JDT transform to a JSON document in order to modify a property in a N-th array element. Is that possible without having to replace the entire element or even the entire array?

{
  "array": [
    {
      name: "A",
      value: 0
    },
    {
      name: "B",
      value: 3.14
    }
  ]
}

是否有一种转换让我了解以下内容?我想更改第二个数组元素,仅更改其值"属性.我不想按名称"搜索它,而是按索引访问.

Is there a transform that gets me to the following? I want to alter the 2nd array element and only its "value" property. I don't want to search for it by "name" but rather access by index.

{
  "array": [
    {
      name: "A",
      value: 0
    },
    {
      name: "B",
      value: 12345678
    }
  ]
}

推荐答案

挑战

使用JSON中的某些库进行转换很容易.如果您的对象称为foo,则主要希望使用foo.array[1].value = "12345678"之类的东西而没有任何循环.

The challenge

It is easy to do your transform with some libraries in JSON. If your object is called foo, you mainly want to something like foo.array[1].value = "12345678" without any kind of looping.

我发现了如何使用SlowCheetah转换Json配置文件中的数组元素?

例如,如果我的基本配置文件具有以下设置:

For example, if my base config file has this setting:

{
  "Settings" :  [1, 2, 3] 
}

我想将其转移到:

{
  "Settings" :  [4, 5, 6] 
}

Collin K 的解决方案是

{
  "@jdt.replace": {
    "@jdt.path": "$.Settings",
    "@jdt.value": [4,5,6]
  }
}

这似乎需要实际替换整个阵列.

进一步挖掘,让我了解到 JDT的未解决问题似乎证实了这一假设.

Digging further let me to an open issue of JDT which seems to confirm this assumption.

我自己还没有使用过JDT,但是我一直在努力使用各种嵌套的JSON,例如与Elasticsearch.

I have not used JDT myself, but I have been struggling with nested JSONs of various kinds e.g. with Elasticsearch.

  • https://github.com/microsoft/json-document-transforms/wiki/Replace-Transformation
  • Using jq to update objects within a JSON document if the value corresponding to a given key starts with a specified string - the JQ way I would use

这篇关于JDT转换以修改第N个数组元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆