bazaarvoice / jolt

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

change name of property in a list to list nested json using JOLT #1180

Closed Prasaddiwalkar closed 1 year ago

Prasaddiwalkar commented 1 year ago

I am news to jolt json transformation. So looking for help in transforming nested json having list of values to nested json with list as following.

Input json

{
  "userData": [
    {
      "userId": "1",
      "age": "20",
      "firstName": "firstname1",
      "lastname": "lastname1",
      "zipCode": "zipcode1",
      "street": "street1",
      "city": "city1",
      "country": "country",
      "gender": "gender1",
      "grade": "grade1",
      "birthday": "birthday1"
    },
    {
      "userId": "2",
      "age": "25",
      "firstName": "firstname2",
      "lastname": "lastname2",
      "zipCode": "zipcode2",
      "street": "street2",
      "city": "city2",
      "country": "country2",
      "gender": "gender2",
      "grade": "grade2",
      "birthday": "birthday2"
    }
  ]
}

Jolt Specification

[
  {
    "operation": "shift",
    "spec": {
      "userData": {
        "*": {
          "userId": "data.[&1].ID",
          "*": "data.[&1].&",
          "zipCode": {
            "#custom-field1": "adr_&1.property",
            "@": "adr_&.value"
          },
          "street": {
            "#custom-field2": "adr_&1.property",
            "@": "adr_&.value"
          },
          "city": {
            "#custom-field3": "adr_&1.property",
            "@": "adr_&.value"
          },
          "country": {
            "#custom-field4": "adr_&1.property",
            "@": "adr_&.value"
          }
        }
      }
    }
  }
]

Output

{
  "data" : [ {
    "ID" : "1",
    "age" : "20",
    "firstName" : "firstname1",
    "lastname" : "lastname1",
    "gender" : "gender1",
    "grade" : "grade1",
    "birthday" : "birthday1"
  }, {
    "ID" : "2",
    "age" : "25",
    "firstName" : "firstname2",
    "lastname" : "lastname2",
    "gender" : "gender2",
    "grade" : "grade2",
    "birthday" : "birthday2"
  } ],
  "adr_zipCode" : {
    "property" : [ "custom-field1", "custom-field1" ],
    "value" : [ "zipcode1", "zipcode2" ]
  },
  "adr_street" : {
    "property" : [ "custom-field2", "custom-field2" ],
    "value" : [ "street1", "street2" ]
  },
  "adr_city" : {
    "property" : [ "custom-field3", "custom-field3" ],
    "value" : [ "city1", "city2" ]
  },
  "adr_country" : {
    "property" : [ "custom-field4", "custom-field4" ],
    "value" : [ "country", "country2" ]
  }
}

Expected json

{
  "data": [
    {
      "ID": "1",
      "age": "20",
      "firstName": "firstname1",
      "lastname": "lastname1",
      "gender": "gender1",
      "grade": "grade1",
      "birthday": "birthday1",
      "address": [
        {
          "property": "custom-field1",
          "value": "zipcode1"
        },
        {
          "property": "custom-field2",
          "value": "street1"
        },
        {
          "property": "custom-field3",
          "value": "city1"
        },
        {
          "property": "custom-field4",
          "value": "country"
        }
      ]
    },
    {
      "ID": "2",
      "age": "25",
      "firstName": "firstname2",
      "lastname": "lastname2",
      "gender": "gender2",
      "grade": "grade2",
      "birthday": "birthday2",
      "address": [
        {
          "property": "custom-field1",
          "value": "zipcode2"
        },
        {
          "property": "custom-field2",
          "value": "street2"
        },
        {
          "property": "custom-field3",
          "value": "city2"
        },
        {
          "property": "custom-field4",
          "value": "country2"
        }
      ]
    }
  ]
}
Prasaddiwalkar commented 1 year ago
[
    {
        "operation": "shift",
        "spec": {
            "userData": {
                "*": {
                    "userId": "data.[&1].ID",
                    "*": "data.[&1].&",
                    "zipCode": {
                        "#custom-field1": "data[#3].address[#2].property",
                        "@": "data[#3].address[#2].value"
                    },
                    "street": {
                        "#custom-field2": "data[#3].address[#2].property",
                        "@": "data[#3].address[#2].value"
                    },
                    "city": {
                        "#custom-field3": "data[#3].address[#2].property",
                        "@": "data[#3].address[#2].value"
                    },
                    "country": {
                        "#custom-field4": "data[#3].address[#2].property",
                        "@": "data[#3].address[#2].value"
                    }
                }
            }
        }
    },
    { // get rid of null components of the array
        "operation": "modify-overwrite-beta",
        "spec": {
            "*": "=recursivelySquashNulls"
        }
    }
]

image