在 json 中按 id 查找元素
Find element by id in json
需要根据元素值进行变换
input.json
{
"Result": {
"owner": {
"name": "test user"
},
"Components": [
{
"id": "123-456-789"
}
],
"123-456-789": {
"temp": {
"emip": "abc",
"teto": "123"
}
}
}
}
变换json
[
{
"operation": "shift",
"spec": {
"Result": {
"Components": {
"*": {
"id": "compId"
}
},
"compId": {
"@": "component"
}
}
}
},
{
"operation": "default",
"spec": {
"compId": null
}
}
]
输出预计为
"123-456-789": {
"temp": {
"emip": "abc",
"teto": "123"
}
}
但结果答案如下,当我用 123-456-789 硬编码该值时,我得到了该值,但我需要动态获取该值。
{
"compId" : "123-456-789"
}
规格
[
{
"operation": "shift",
"spec": {
"Result": {
"Components": {
"*": { // components array index
"id": {
"*": { // match any value of id
// go back up the tree 5 levels, and then
// lookup a key based on the value of the "id"
// and write that to the output at at that same
// key
"@(4,&)": "&1"
}
}
}
}
}
}
}
]
需要根据元素值进行变换 input.json
{
"Result": {
"owner": {
"name": "test user"
},
"Components": [
{
"id": "123-456-789"
}
],
"123-456-789": {
"temp": {
"emip": "abc",
"teto": "123"
}
}
}
}
变换json
[
{
"operation": "shift",
"spec": {
"Result": {
"Components": {
"*": {
"id": "compId"
}
},
"compId": {
"@": "component"
}
}
}
},
{
"operation": "default",
"spec": {
"compId": null
}
}
]
输出预计为
"123-456-789": {
"temp": {
"emip": "abc",
"teto": "123"
}
}
但结果答案如下,当我用 123-456-789 硬编码该值时,我得到了该值,但我需要动态获取该值。
{
"compId" : "123-456-789"
}
规格
[
{
"operation": "shift",
"spec": {
"Result": {
"Components": {
"*": { // components array index
"id": {
"*": { // match any value of id
// go back up the tree 5 levels, and then
// lookup a key based on the value of the "id"
// and write that to the output at at that same
// key
"@(4,&)": "&1"
}
}
}
}
}
}
}
]