Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merging two arrays using one common key #1239

Open
Vignesh-Muthusamy05 opened this issue Mar 6, 2024 · 1 comment
Open

Merging two arrays using one common key #1239

Vignesh-Muthusamy05 opened this issue Mar 6, 2024 · 1 comment

Comments

@Vignesh-Muthusamy05
Copy link

Vignesh-Muthusamy05 commented Mar 6, 2024

Hi, I am stuck in generating an array with fields from two different arrays which share a common key. When the common key's value is the same in both array elements, it has to be merged. Below is the sample Input and Output JSON. Please help me in writing a JOLT spec for this scenario. Your help is much appreciated!

Input JSON
{
"parentArray": [
{
"array1": [
{
"field1": "1008",
"field2": "b1111"
},
{
"field1": "2345",
"field2": "b2222"
}
],
"array2": [
{
"field1": "1008"
},
{
"field1": "3210"
}
]
}
]
}

Output JSON
{
"parentResultArray": [{
"resultArray": [{
"field1": "1008",
"jsonObject": {
"field2": "b1111"
}
}]
}]
}

@gbouget
Copy link

gbouget commented Mar 9, 2024

Is it possible that there are also values in array2? If not, this solution should be suitable:

[
  { // Creation of arrayTmp to index field1 values in array1
    "operation": "shift",
    "spec": {
      "parentArray": {
        "*": {
          "array1": {
            "*": {
              "field1": "arrayTmp.@0.&",
              "*": "arrayTmp.@(1,field1).jsonObject.&"
            }
          },
          "array2": {
            "*": {
              "*": "&2[&1].&"
            }
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "array2": {
        "*": {
          "field1": {
            "*": { // value of field1
              "@(5,arrayTmp.&)": "resultArray[]" // & refers to value field1
                                                 // @5 moves up 5 levels to reach root node
            }
          }
        }
      }
    }
  }
]

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants