如何使用JSONata来改变JSON响应的重组。

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

How to use JSONata to change JSON response restructure

问题

{
	"output": [
		{
			"enterpriseid": 53898,
			"enterprise": "Absolute F and B Facilities Management LLC",
			"bookingvalue": 520.5
		},
		{
			"enterpriseid": 53899,
			"enterprise": "testing Buyer",
			"bookingvalue": 200
		}
	]
}
英文:

I have one JSON data that I want to restructure the data using JSONata query

{
    "response": [
        {
            "enterpriseid": 53898,
            "enterprise": " Absolute F and B Facilities Management LLC ",
            "bookingvalue": 46
        },
        {
           
            "enterpriseid": 53898,
            "enterprise": " Absolute F and B Facilities Management LLC ",
            "bookingvalue": 275
        },
        {
            "enterpriseid": 53898,
            "enterprise": " Absolute F and B Facilities Management LLC ",
            "bookingvalue": 199.5
            
        },
		{
            "enterpriseid": 53899,
            "enterprise": " testing Buyer ",
            "bookingvalue": 200
            
        }
       
      
    ]
}

And I'm trying to use JSONata to change it to something more like this:

{
	"output": [
		{
			"enterpriseid": 53898,
			"enterprise": " Absolute F and B Facilities Management LLC ",
			"bookingvalue": 520.5
		},
		{
			"enterpriseid": 53899,
			"enterprise": " testing Buyer",
			"bookingvalue": 200
		}
	]
}

i tried with this query but i am getting only enterpriseid and enterprise not getting bookingvalue

{
  
  "output": $distinct(response.{
    "enterpriseid" : enterpriseid,
    "enterprise": enterprise,
    "bookingvalue": $sum($filter(response, function($item) {$item.enterpriseid = enterpriseid}).bookingvalue)
  } )
}

答案1

得分: 0

您需要使用双美元符号($$来跳出$distinct的上下文,以对所有筛选项的总和进行计算。

$$ 输入JSON的根。只有在需要暂时导航到不同路径时才需要。

例如:

{
  "output": $distinct(response.{
    "enterpriseid": enterpriseid,
    "enterprise": enterprise,
    "bookingvalue": $sum($filter($$.response, function($item) {$item.enterpriseid = enterpriseid}).bookingvalue)
  })
}

(请注意,在$filter内部使用了双美元符号$$。)

这将输出:

{
  "output": [
    {
      "enterpriseid": 53898,
      "enterprise": " Absolute F and B Facilities Management LLC ",
      "bookingvalue": 520.5
    },
    {
      "enterpriseid": 53899,
      "enterprise": " testing Buyer ",
      "bookingvalue": 200
    }
  ]
}

工作示例: https://try.jsonata.org/Y-7YTZ_eJ

英文:

You need to use double dollar signs ($$) to break out of $distinct's context for the sum of all filtered items.

> $$ The root of the input JSON. Only needed if you need to break out of
> the current context to temporarily navigate down a different path.

For example:

{
  "output": $distinct(response.{
    "enterpriseid" : enterpriseid,
    "enterprise": enterprise,
    "bookingvalue": $sum($filter($$.response, function($item) {$item.enterpriseid = enterpriseid}).bookingvalue)
  })
}

(Note the double dollar sign $$ usage inside of $filter.)

Which outputs:

{
  "output": [
    {
      "enterpriseid": 53898,
      "enterprise": " Absolute F and B Facilities Management LLC ",
      "bookingvalue": 520.5
    },
    {
      "enterpriseid": 53899,
      "enterprise": " testing Buyer ",
      "bookingvalue": 200
    }
  ]
}

Working Example: https://try.jsonata.org/Y-7YTZ_eJ

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

发表评论

匿名网友

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

确定