bazaarvoice / jolt

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

Need to add an attribute : "includeEmpResponse": true, to the input JSON structure in an array for a particular element using Jolt's spec #1238

Closed naveenpi closed 3 months ago

naveenpi commented 7 months ago

Input:

{
  "orgData": [
    {
      "orgId": "1",
      "emplId": "23"
    },
    {
      "orgId": "2",
      "emplId": "24"
    }
  ]
}

Expected Output:

{
  "orgData": [
    {
      "orgId": "1",
      "emplId": "23"
    },
    {
      "orgId": "2",
      "emplId": "24",
      "includeEmpResponse": true
    }
  ]
}

I tried using the below Jolt's spec but the new element was not adding up, please help me in this regard:

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "orgData": {
        "*": {
          "orgId": {
            "2": {
              "includeEmpResponse": true
            }
          }
        }
      }
    }
  }
]
gbouget commented 7 months ago
[
  {
    "operation": "shift",
    "spec": {
      "orgData": {
        "*": {
          "*": "orgData[&1].&",
          "orgId": {
            "*": { // all matches except 2
              "@1": "orgData[&3].orgId" // @1 moves up one level in the tree
            },
            "2": { // only 2
              "@3": { // to move up 3 levels (eq line 6, under orgData)
                "#true": "orgData[&4].includeEmpResponse",
                "@2": "orgData[&4].orgId" // @2 reaches line 12, value "2"
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "orgData": {
        "*": {
          "includeEmpResponse": "=toBoolean"
        }
      }
    }
  }
]

image

naveenpi commented 7 months ago

Thanks for your reply, when i was trying to add few more modifications on the output Json, it was not working:

Input JSON

{
  "orgData": [
    {
      "orgId": "1",
      "emplId": "23"
    },
    {
      "orgId": "2",
      "emplId": "24"
    }
  ],
  "code": "123"
}

The Expected output is

{
  "org_data": [
    {
      "org_id": "1",
      "empl_id": "23"
    },
    {
      "org_id": "2",
      "empl_id": "24",
      "includeEmpResponse": true
    }
  ],
  "code": "123"
}

SPEC:

[
  {
    "operation": "shift",
    "spec": {
      "orgData": {
        "*": {
          "orgId": "org_data[&1].org_id",
          "emplId": "org_data[&1].empl_id"
        }
      },
      "org_data": {
        "*": {
          "*": "org_data[&1].&",
          "org_id": {
            "*": { // all matches except 2
              "@1": "org_data[&3].org_id" // @1 moves up one level in the tree
            },
            "2": { // only 2
              "@3": { // to move up 3 levels (eq line 6, under orgData)
                "#true": "org_data[&4].includeEmpResponse",
                "@2": "org_data[&4].org_id" // @2 reaches line 12, value "2"
              }
            }
          }
        }
      },
      "subcode": "subcode"
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "org_data": {
        "*": {
          "includeEmpResponse": "=toBoolean"
        }
      }
    }
  }
]
gbouget commented 7 months ago
[
  {
    "operation": "shift",
    "spec": {
      "*": "&", // map all attributes (except orgData) identically
      "orgData": {
        "*": {
          "emplId": "org_data[&1].empl_id",
          "orgId": {
            "*": { // all matches except 2
              "@1": "org_data[&3].org_id" // @1 moves up one level in the tree
            },
            "2": { // only 2
              "@3": { // to move up 3 levels (eq line 6, under orgData)
                "@2": "org_data[&4].org_id", // @2 reaches line 12, value "2"
                "#true": "org_data[&4].includeEmpResponse"
              }
            }
          }
        }
      }
    }
  },
  { // input is the result of the previous operation
    "operation": "modify-overwrite-beta",
    "spec": {
      "org_data": {
        "*": {
          "includeEmpResponse": "=toBoolean"
        }
      }
    }
  }
]
naveenpi commented 7 months ago

Using this spec, how does emplId comes in the output even without referncing in the spec and by orgData[&3], orgData[&4] are we trying to access the elements 3, 4 in orgData array? if so they are only 2 elements in the array right how it is accesssing 3,4 from array?

[
  {
    "operation": "shift",
    "spec": {
      "orgData": {
        "*": {
          "*": "orgData[&1].&",
          "orgId": {
            "*": { // all matches except 2
              "@1": "orgData[&3].orgId" // @1 moves up one level in the tree
            },
            "2": { // only 2
              "@3": { // to move up 3 levels (eq line 6, under orgData)
                "#true": "orgData[&4].includeEmpResponse",
                "@2": "orgData[&4].orgId" // @2 reaches line 12, value "2"
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "orgData": {
        "*": {
          "includeEmpResponse": "=toBoolean"
        }
      }
    }
  }
]
gbouget commented 6 months ago

The mapping of empId is expressed with the symbols '*' and '&'.

//...
    "spec": {
      "orgData": {
        "*": { // '*' just below ogData is here
          "*": "orgData[&1].&", // everything (including empId) matches except orgId
          "orgId": { // orgId doesn't correspond with '*' above because it's specified here
//...
          }
        }
//...

orgData[&3], orgData[&4] are arrays whose grouping is based on the node 3 and 4 levels above: i.e. the '*' just below ogData.

naveenpi commented 3 months ago

thank you