bazaarvoice / jolt

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

Need help to figure out a solution using JOLT #875

Open chabett opened 5 years ago

chabett commented 5 years ago

Pardon me for posting this as an issue to start with. Let me know what the right forum is.

Input JSON: [{ "NM108": "XX", "NM109": "123123123", "NM102": "2", "id": "NM1", "NM103": "ABCD WXYZ", "NM101": "85" }, { "N301": "1234 MAIN ST", "id": "N3" }, { "N401": "QWEQWRWEE", "id": "N4", "N402": "AB", "N403": "12341234123" }, { "NM108": "AM", "NM109": "345345345", "NM104": "Kqwer", "NM102": "1", "id": "NM1", "NM103": "Touok", "NM101": "LI" }, { "N301": "1369 Riddley Ripper Blvd", "id": "N3" } ] Problem:
This is a part of a big edi 837 standard input. Using JOLT script, we need to find only the N301 node (i.e. node with value "1234 MAIN ST") that comes right after NM101=85.

With my limited knowledge of JOLT, I can't seem to find a way to only find that N301 node where value is "1234 MAIN ST". All i can get is an array of "1234 MAIN ST" and "1369 Riddley Ripper Blvd". I can't seem to find a way to restrict the look up to only one that is a sibling of a particular node.

Is this an issue with JOLT engine? Any possible solution?

wisthy commented 5 years ago

Hello,

Would be nice to share also the spec you have tried so far. So that we don't have to start from scratch if we don't have to :)

wisthy commented 5 years ago

Ok, here goes my attempt of solution. Jolt allows easily to find a sibling based on node name but what you need is to find two nodes based on contents of each of them. That requires some tricks. I chose to based my logic on the fact that you assumed that the node to display is always right after the one identified.

So,

  1. add index number of each node as a value inside it (to make it easier to deal with)
  2. find the index of the node containing value "NM101 = 85"
  3. add 1 to that index
  4. display content of the node to the new index
  5. remove the temporary field
[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "@": "tmp.[&1]",
        "$": "tmp.[&1]._id"
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "tmp": {
        "*": {
          "NM101": {
            "85": {
              "@(2,_id)": "_found",
              "@(2,NM101)": "tmp.[&3].NM101"
            },
            "*": {
              "@(2,NM101)": "tmp.[&3].NM101"
            }
          },
          "*": "tmp.[&1].&"
        }
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "\\_found": "=intSum(@(1,_found), 1)"
    }
  }
  ,
  {
    "operation": "shift",
    "spec": {
      "_found": {
        "*": {
          "@(2,tmp[&])": ""
        }
      }
    }
    },
  {
    "operation": "shift",
    "spec": {
      "_id": null,
      "*": "&"
    }
    }
]

which gives as output:

{
  "N301" : "1234 MAIN ST",
  "id" : "N3"
}