bazaarvoice / jolt

JSON to JSON transformation library written in Java.
Apache License 2.0
1.56k stars 330 forks source link

Merge Array - Insert an element if the value matches #1112

Closed adityakonda closed 2 years ago

adityakonda commented 2 years ago

I'm new to JOLT, I'm stuck with this for 10 days. will really appreciate it if anyone can solve this puzzle.

PROBLEM STATEMENT: In the below input and expected output, if authrzd_new. authrzd_mbr_id matches with authrzd. authrzd_mbr_id then the enty_func must be moved to the matched authrzd_mbr_id


------ JSON INPUT


{
   "authrzd_new":{
         "authrzd_mbr_id":"102",
         "enty_func":[
            {
               "enty_func_id":"10",
               "func_typ":"VIEW",
               "enty_typ":"EDIT_TEST",
               "auth_acs_ind":"1",
               "src_commit_ts":"20211028170625.689991"
            }
         ]
      },
   "authrzd":[
      {
         "authrzd_mbr_id":"101",
         "enty_func":[
            {
               "enty_func_id":"20",
               "func_typ":"EDIT",
               "enty_typ":"CONTACT",
               "auth_acs_ind":"1",
               "src_commit_ts":"20211028170625.689991"
            }
         ]
      },
      {
         "authrzd_mbr_id":"102",
         "enty_func":[
            {
               "enty_func_id":"20",
               "func_typ":"EDIT",
               "enty_typ":"CONTACT",
               "auth_acs_ind":"1",
               "src_commit_ts":"20211028170625.689991"
            }
         ]
      }
   ]
}

------ EXPECTED OUT


{
   "authrzd":[
      {
         "authrzd_mbr_id":"101",
         "enty_func":[
            {
               "enty_func_id":"20",
               "func_typ":"EDIT",
               "enty_typ":"CONTACT",
               "auth_acs_ind":"1",
               "src_commit_ts":"20211028170625.689991"
            }
         ]
      },
      {
         "authrzd_mbr_id":"102",
         "enty_func":[
            {
               "enty_func_id":"20",
               "func_typ":"EDIT",
               "enty_typ":"CONTACT",
               "auth_acs_ind":"1",
               "src_commit_ts":"20211028170625.689991"
            },
            {
               "enty_func_id":"10",
               "func_typ":"VIEW",
               "enty_typ":"EDIT_TEST",
               "auth_acs_ind":"1",
               "src_commit_ts":"20211028170625.689991"
            }
         ]
      }
   ]
 }

------ MY Work In Progress - JOLT SPEC


[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "authrzd_mbr_id": {
            "@(enty_func)": "authrzd[#2].enty_func",
            "102": {  // working on how to pass this value dynamically. 
              "@(2,authrzd_mbr_id)": "authrzd[].authrzd_mbr_id",
              "@(2,enty_func)": "authrzd[#2].enty_func"
            }
          }
        }
      }
    }
  }
]

``` @milosimpson 
adityakonda commented 2 years ago

@milosimpson, I looked at the previous examples, none of them were helpful.

kubilayeksioglu commented 2 years ago

@adityakonda If you're operating with IDs, it's always a good idea to go for a map convention first. Try following approach:

[
  {
    "operation": "shift",
    "spec": {
      "authrzd": {
        "*": {
          "@": "data.@(authrzd_mbr_id)"
        }
      },
      "authrzd_new": {
        "enty_func": {
          "*": "data.@(2,authrzd_mbr_id).enty_func.[]"
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "data": {
        "*": "authrzd.[]"
      }
    }
  }
]
adityakonda commented 2 years ago

THANK YOU SO MUCH @kubilayeksioglu ...!!! You saved my day :)... I have was wondering how will the update work, let say we have to replace the _authrz_new. authrzd_mbr_id.entyfunc with _authrz. authrzd_mbr_id.entyfunc.. Can you let me know how this works?


------ JSON INPUT

{
  "primary_key": "101_ABC",
  "authrzd_new": {
    "authrzd_mbr_id": "102",
    "enty_func": [
      {
        "enty_func_id": "10",
        "func_typ": "VIEW - UPDATED",
        "enty_typ": "EDIT_TEST - UPDATED",
        "auth_acs_ind": "0",
        "src_commit_ts": "20211028170625.689991"
      }
    ]
  },
  "authrzd": [
    {
      "authrzd_mbr_id": "101",
      "enty_func": [
        {
          "enty_func_id": "20",
          "func_typ": "EDIT",
          "enty_typ": "CONTACT",
          "auth_acs_ind": "1",
          "src_commit_ts": "20211028170625.689991"
        }
      ]
    },
    {
      "authrzd_mbr_id": "102",
      "enty_func": [
        {
          "enty_func_id": "20",
          "func_typ": "EDIT",
          "enty_typ": "CONTACT",
          "auth_acs_ind": "1",
          "src_commit_ts": "20211028170625.689991"
        },
        {
          "enty_func_id": "10",
          "func_typ": "VIEW",
          "enty_typ": "EDIT_TEST",
          "auth_acs_ind": "1",
          "src_commit_ts": "20211028170625.689991"
        }
      ]
    }
  ]
}

------ JSON OUTPUT


{
  "primary_key": "101_ABC",
  "authrzd": [
    {
      "authrzd_mbr_id": "101",
      "enty_func": [
        {
          "enty_func_id": "20",
          "func_typ": "EDIT",
          "enty_typ": "CONTACT",
          "auth_acs_ind": "1",
          "src_commit_ts": "20211028170625.689991"
        }
      ]
    },
    {
      "authrzd_mbr_id": "102",
      "enty_func": [
        {
          "enty_func_id": "20",
          "func_typ": "EDIT",
          "enty_typ": "CONTACT",
          "auth_acs_ind": "1",
          "src_commit_ts": "20211028170625.689991"
        },
        {
          "enty_func_id": "10",
          "func_typ": "VIEW - UPDATED",
          "enty_typ": "EDIT_TEST - UPDATED",
          "auth_acs_ind": "0",
          "src_commit_ts": "20211028170625.689991"
        }
      ]
    }
  ]
}
gauraverma1990 commented 2 years ago

@kubilayeksioglu @adityakonda Hi Guys, I am facing an issue related to Jolt, it seems some configuration issue to me. Its just simple conversion with spec file present on google cloud not in the project. Could you please have a look and help me. Below is the link for issue details: https://github.com/bazaarvoice/jolt/issues/1113