如何使用JSONata变换操作更新单个对象属性

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

How to update single object property using JSONata transform operator

问题

我正在尝试使用JSONata transform 运算符来更新对象中的单个属性。我不希望丢失初始对象中的任何其他属性。

我的输入对象:

{
  "item": {
    "_id": "097369183479824602",
    "name": "Name",
    "description": "Description",
    "sku": "0230115",
    "quantity": 8
  },
  "quantityRemaining": 6
}

我的JSONata表达式:

$ ~> | item | { "quantity" : quantityRemaining } |

预期输出:

{
  "item": {
    "_id": "097369183479824602",
    "name": "Name",
    "description": "Description",
    "sku": "0230115",
    "quantity": 6
  },
  "quantityRemaining": 6
}

但实际对象是:

{
  "item": {
    "_id": "097369183479824602",
    "name": "Name",
    "description": "Description",
    "sku": "0230115",
    "quantity": 8
  },
  "quantityRemaining": 6
}

我不确定为什么item.quantity的值没有被quantityRemaining覆盖。 感谢任何帮助!

有趣的是,如果我将表达式简化为以下内容:

$ ~> | item | { "quantity" : 3 } |

输出如预期:

{
  "item": {
    "_id": "097369183479824602",
    "name": "Name",
    "description": "Description",
    "sku": "0230115",
    "quantity": 3
  },
  "quantityRemaining": 6
}

我在https://try.jsonata.org/播放器中进行所有这些操作。

英文:

I'm trying to update a single property within an object using the JSONata transform operator. I don't want to lose any other properties from the initial object.

My input object:

{
  "item": {
    "_id": "097369183479824602",
    "name": "Name",
    "description": "Description",
    "sku": "0230115",
    "quantity": 8
  },
  "quantityRemaining": 6
}

My JSONata expression:

$ ~> | item | { "quantity" : quantityRemaining } |

Expected output:

{
  "item": {
    "_id": "097369183479824602",
    "name": "Name",
    "description": "Description",
    "sku": "0230115",
    "quantity": 6
  },
  "quantityRemaining": 6
}

But the actual object is:

{
  "item": {
    "_id": "097369183479824602",
    "name": "Name",
    "description": "Description",
    "sku": "0230115",
    "quantity": 8
  },
  "quantityRemaining": 6
}

I'm not sure why the value of item.quantity is not being overwritten by quantityRemaining.
Any help is appreciated!

Interestingly, if I simply the expression to the following:

$ ~> | item | { "quantity" : 3 } |

the output is expected:

{
  "item": {
    "_id": "097369183479824602",
    "name": "Name",
    "description": "Description",
    "sku": "0230115",
    "quantity": 3
  },
  "quantityRemaining": 6
}

I'm doing all of this in the https://try.jsonata.org/ playground.

答案1

得分: 1

在你的表达式中 - 当你尝试设置 quantity 时 - 上下文被设置为 item 的值。input.item.quantityRemaining 求值为 undefined,因此 JSONata 似乎会丢弃表达式中的值,并使用输入中的值。

你可以使用 $$ 变量,它引用输入 JSON 的根,以跳出当前的上下文。

JSONata playground 中可以看到这个示例的工作方式。

请注意,我对 JSONata 不是非常熟悉,所以可能有更好的方法来实现你想要的效果。

英文:

In your expression - where you try and set the quantity - the context is set to the value of item. input.item.quantityRemaining evaluates to undefined and so JSONata seems to discard the value from the expression and use the one from the input.

You could use the $$ variable, which reference the root of the input JSON, to break out of the current context.

https://docs.jsonata.org/programming#built-in-variables

$ ~> | item | { "quantity" : $$.quantityRemaining } |

You can see this working in this JSONata playground.

Note that I'm not super familiar with JSONata, and so there might be a better way to achieve what you're after.

huangapple
  • 本文由 发表于 2023年6月9日 03:43:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76435226.html
匿名

发表评论

匿名网友

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

确定