JOLT:按属性选择/更改子项

huangapple go评论82阅读模式
英文:

JOLT: select/change child by attribute

问题

我对JOLT还很陌生,并进行了很多搜索(可能使用了错误的搜索词),但无法找到解决我看起来很简单的问题的答案。
我想要更改由另一个属性标识的子项的属性。

所以我有这个输入:

{
  "Level0": {
    "Level-1": {
      "ID": 10,
      "Status": "OK"
    },
    "Level-2": {
      "ID": 20,
      "Status": "OK"
    },
    "Level-3": {
      "ID": 30,
      "Status": "OK"
    }
  }
}

并希望更改ID=20的子项的'Status'值,以便获得预期输出:

{
  "Level0": {
    "Level-1": {
      "ID": 10,
      "Status": "OK"
    },
    "Level-2": {
      "ID": 20,
      "Status": "Running"
    },
    "Level-3": {
      "ID": 30,
      "Status": "OK"
    }
  }
}

我尝试了许多使用"modify-overwrite-beta"的变体,但无法获得所需的结果。

可以有人帮助吗?

英文:

I'm new to JOLT and googled a lot (maybe using wrong search terms) but could not find an answer to my seemingly simple problem.
I want to change an attribute of a child which is identified by another attribute.

So I have this input:

{
  "Level0": {
    "Level-1": {
      "ID": 10,
      "Status": "OK",
    },
    "Level-2": {
      "ID": 20,
      "Status": "OK",
    },
    "Level-3": {
      "ID": 30,
      "Status": "OK",
    }
  }
}

and want to change the 'Status' value of the child with ID=20 so that this is the expected output:

{
  "Level0": {
    "Level-1": {
      "ID": 10,
      "Status": "OK",
    },
    "Level-2": {
      "ID": 20,
      "Status": "Running",
    },
    "Level-3": {
      "ID": 30,
      "Status": "OK",
    }
  }
}

I tried many variants with "modify-overwrite-beta" but could not get the desired result.

Can someone help please?

答案1

得分: 0

你可以在shift转换规范中使用条件,例如:

[
  {
    "operation": "shift",
    "spec": {
      "*": { // 最外层级
        "*": {
          "ID": {
            "@": "&3.&2.&1", // 复制ID的值
            "20": { "#Running": "&4.&3.Status" }, // 通过前缀#表示固定值
            "*": { "@2,Status": "&4.&3.Status" } // 如果ID不等于20,则保持“Status”的当前值
          }
        }
      }
    }
  }
]

其中带有*和&符号(&1&2...)分别表示沿着树上升相应层级时接收到的节点的名称。

该站点http://jolt-demo.appspot.com/上的演示是:

JOLT:按属性选择/更改子项

英文:

You can use a conditional within a shift transformation specification such as

[
  {
    "operation": "shift",
    "spec": {
      "*": { // the outermost level 
        "*": {
          "ID": {
            "@": "&3.&2.&1", // replicate the ID's value
            "20": { "#Running": "&4.&3.Status" }, // express fixed value by prefixing it with #
            "*": { "@2,Status": "&4.&3.Status" } // if ID is not equal to 20, the keep the current value of the "Status"
          }
        }
      }
    }
  }
]

where the identifiers with ampersands( &1,&2... ) represent the name of the nodes received by going that much level up the tree respectively.

the demo on the site http://jolt-demo.appspot.com/ is :

JOLT:按属性选择/更改子项

huangapple
  • 本文由 发表于 2023年7月24日 16:18:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76752583.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定