英文:
Merging JSONs based on condition
问题
请帮助我获得以下输出。
基于Input1和Input2,我需要获取输出。如果ID匹配,则需要使用PATCH方法,否则需要使用POST方法。
如果ID("ID":"123456")在Input1中不存在,则使用POST,否则使用PATCH。
Input1:
{
"prod": [
{
"Id": "123456",
"value": "ABC"
},
{
"Id": "123456",
"value": "DEF"
},
{
"Id": "987654",
"value": "DEF"
}
]
}
Input2:
{
"ProdInfo": {
"Prod": [
{
"Id": "123456",
"value1": "LMN"
},
{
"Id": "123456",
"value1": "OPQ"
},
{
"Id": "654321",
"value1": "OPQ"
}
]
}
}
Output:
{
"output": [
{
"Request": [
{
"method": "PATCH",
"body": {
"ID": "123456",
"Value": "ABC;DEF;LMN;OPQ"
}
},
{
"method": "POST",
"body": {
"ID": "654321",
"Value": "OPQ"
}
}
]
}
]
}
英文:
Please help me to get the below output.
Based on Input1 and Input2 I have get the output. If Id matches then I need to have PATCH method if it does not then I need to have POST method.
If the Id ("ID": "123456") does not exist in input1 then it is POST else it is PATCH.
**Input1: **
{
"prod": [
{
"Id": "123456",
"value": "ABC"
},
{
"Id": "123456",
"value": "DEF"
},
{
"Id": "987654",
"value": "DEF"
}
]
}
Input2:
{
"ProdInfo": {
"Prod": [
{
"Id": "123456",
"value1": "LMN"
},
{
"Id": "123456",
"value1": "OPQ"
},
{
"Id": "654321",
"value1": "OPQ"
}
]
}
}
Output:
{
"output": [
{
"Request": [
{
"method": "PATCH",
"body": {
"ID": "123456",
"Value": "ABC;DEF;LMN;OPQ"
}
},
{
"method": "POST",
"body": {
"ID": "654321",
"Value": "OPQ"
}
}
]
}
]
}
答案1
得分: 0
你可以通过按id分组的方式来解决这个问题,然后使用pluck()来获取键值,以便映射到所需的输出方式。我添加了一个函数来封装一些内部复杂性。
%dw 2.0
output application/json
var input1={
"prod": [
{
"Id": "123456",
"value": "ABC"
},
{
"Id": "123456",
"value": "DEF"
}
]
}.prod groupBy ($.Id)
fun joinValues(v1,v2)=(v1 default [] ++ v2 default []) joinBy ";"
---
{
"output": [
{
Request:
payload.ProdInfo.Prod
groupBy ($.Id)
pluck ((value, key, index) -> {
method: if (namesOf(input1) contains (key as String)) "PATCH" else "POST",
body: {
ID: key,
Value: joinValues(input1[key as String].*value, value.*value1)
}
})
}
]
}
输入负载(等同于input2)
{
"ProdInfo": {
"Prod": [
{
"Id": "123456",
"value1": "LMN"
},
{
"Id": "123456",
"value1": "OPQ"
},
{
"Id": "654321",
"value1": "OPQ"
}
]
}
}
输出:
{
"output": [
{
"Request": [
{
"method": "PATCH",
"body": {
"ID": "123456",
"Value": "ABC;DEF;LMN;OPQ"
}
},
{
"method": "POST",
"body": {
"ID": "654321",
"Value": "OPQ"
}
}
]
}
]
}
英文:
You can approach this problem by grouping by id, then use pluck() to get the key values in a way that can be mapped into the desired output. I added a function to encapsulate some of the inner complexity.
%dw 2.0
output application/json
var input1={
"prod": [
{
"Id": "123456",
"value": "ABC"
},
{
"Id": "123456",
"value": "DEF"
}
]
}.prod groupBy ($.Id)
fun joinValues(v1,v2)=(v1 default [] ++ v2 default []) joinBy ";"
---
{
"output": [
{
Request:
payload.ProdInfo.Prod
groupBy ($.Id)
pluck ((value, key, index) -> {
method: if (namesOf(input1) contains (key as String)) "PATCH" else "POST",
body: {
ID: key,
Value: joinValues(input1[key as String].*value, value.*value1)
}
})
}
]
}
Input payload (equal to input2)
{
"ProdInfo": {
"Prod": [
{
"Id": "123456",
"value1": "LMN"
},
{
"Id": "123456",
"value1": "OPQ"
},
{
"Id": "654321",
"value1": "OPQ"
}
]
}
}
Output:
{
"output": [
{
"Request": [
{
"method": "PATCH",
"body": {
"ID": "123456",
"Value": "ABC;DEF;LMN;OPQ"
}
},
{
"method": "POST",
"body": {
"ID": "654321",
"Value": "OPQ"
}
}
]
}
]
}
答案2
得分: 0
我希望这对你有所帮助!
代码:
<!-- 开始片段: js 隐藏: false 控制台: true Babel: false -->
<!-- 语言: lang-html -->
%dw 2.0
var prods = (input1.prod ++ input2.ProdInfo.Prod) map ((item, index) -> {Id: item.Id, value: if (item.value1 != null) item.value1 else item.value})
var ids = prods.Id distinctBy $
var prodsGrouped = ids map (id, index) -> (prods filter ($.Id == id))
output application/json
---
{
"output": [
"Request": prodsGrouped map {
"method": if (sizeOf($.Id) == 1) "POST" else "PATCH",
"Body": {
"ID": $.Id[0],
"Value ": $.value joinBy ";"
}
}
]
}
输出:
<!-- 开始片段: js 隐藏: false 控制台: true Babel: false -->
<!-- 语言: lang-js -->
{
"output": [
{
"Request": [
{
"method": "PATCH",
"Body": {
"ID": "123456",
"Value ": "ABC;DEF;LMN;OPQ"
}
},
{
"method": "POST",
"Body": {
"ID": "987654",
"Value ": "DEF"
}
},
{
"method": "POST",
"Body": {
"ID": "654321",
"Value ": "OPQ"
}
}
]
}
]
}
如果你需要进一步的帮助,请随时告诉我。
英文:
i hope this helps you!
Code:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
%dw 2.0
var prods = (input1.prod ++ input2.ProdInfo.Prod) map ((item, index) -> {Id: item.Id, value: if (item.value1 != null) item.value1 else item.value})
var ids = prods.Id distinctBy $
var prodsGrouped = ids map (id, index) -> (prods filter ($.Id == id))
output application/json
---
{
"output": [
"Request": prodsGrouped map {
"method": if (sizeOf($.Id) == 1) "POST" else "PATCH",
"Body": {
"ID": $.Id[0],
"Value ": $.value joinBy ";"
}
}
]
}
<!-- end snippet -->
Output
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
{
"output": [
{
"Request": [
{
"method": "PATCH",
"Body": {
"ID": "123456",
"Value ": "ABC;DEF;LMN;OPQ"
}
},
{
"method": "POST",
"Body": {
"ID": "987654",
"Value ": "DEF"
}
},
{
"method": "POST",
"Body": {
"ID": "654321",
"Value ": "OPQ"
}
}
]
}
]
}
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论