合并JSONs基于条件

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

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) -&gt; {Id: item.Id, value: if (item.value1 != null) item.value1 else item.value})
var ids = prods.Id distinctBy $
var prodsGrouped = ids map (id, index) -&gt; (prods filter ($.Id == id))
output application/json  
---
{
  &quot;output&quot;: [

      &quot;Request&quot;: prodsGrouped map {
        &quot;method&quot;: if (sizeOf($.Id) == 1) &quot;POST&quot; else &quot;PATCH&quot;,
        &quot;Body&quot;: {
          &quot;ID&quot;: $.Id[0],
          &quot;Value  &quot;: $.value joinBy &quot;;&quot;
        }
      }
  ]
}

<!-- end snippet -->

Output

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

{
  &quot;output&quot;: [
    {
      &quot;Request&quot;: [
        {
          &quot;method&quot;: &quot;PATCH&quot;,
          &quot;Body&quot;: {
            &quot;ID&quot;: &quot;123456&quot;,
            &quot;Value  &quot;: &quot;ABC;DEF;LMN;OPQ&quot;
          }
        },
        {
          &quot;method&quot;: &quot;POST&quot;,
          &quot;Body&quot;: {
            &quot;ID&quot;: &quot;987654&quot;,
            &quot;Value  &quot;: &quot;DEF&quot;
          }
        },
        {
          &quot;method&quot;: &quot;POST&quot;,
          &quot;Body&quot;: {
            &quot;ID&quot;: &quot;654321&quot;,
            &quot;Value  &quot;: &quot;OPQ&quot;
          }
        }
      ]
    }
  ]
}

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年3月10日 00:45:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75687614.html
匿名

发表评论

匿名网友

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

确定