Home > Software design >  what will be the jolt transform for that?
what will be the jolt transform for that?

Time:10-31

I want the jolt transform for the given input . Your help in this is highly appreciated . thanks i am providing the input and expected output. in input json Photos array is dynamic in nature. Here it is 3 , it can be 3 or 4 or5 any .

INPUT JSON .

{
  "Entity": {
    "card": {
     "cardNo":"123456789",
      "cardStatus":"10",
      "cardAddress":"UK",
      "cardAddress1":"US",
      "cardCity":"mk" ,
       "name": "RAM",
      "lastName": "ABU",
       "name1": "RAM1",
      "lastName1": "ABU1"
    },
    "Photos": [
      {
        "Id": "327703",
        "Caption": "TEST>> photo 1",
        "Url": "http://bob.com/0001/327703/photo.jpg"
      },
      {
        "Id": "327704",
        "Caption": "TEST>> photo 2",
        "Url": "http://bob.com/0001/327704/photo.jpg"
      },
      {
        "Id": "327704",
        "Caption": "TEST>> photo 2",
        "Url": "http://bob.com/0001/327704/photo.jpg"
      }
    ]
  }
}

OUTPUT GETTING after jolt transform

{
  "tab": {
    "text": "123456789"
  },
  "address": [
    {
      "add": "UK",
      "add2": "US",
      "city": "mk"
    }
  ],
  "Photos": [
    {
      "no": "327703",
      "caption": "TEST>> photo 1"
    },
    {
      "no": "327704",
      "caption": "TEST>> photo 2"
    },
    {
      "no": "327704",
      "caption": "TEST>> photo 2"
    }
  ]
}

WHAT WILL BE THE CORRECT JOLT TRANSFORM FOR THIS?

jolt spec that i have used IS

[
  {
    "operation": "shift",
    "spec": {
      "Entity": {
        "card": {
          "cardNo": "tab.text",
          "cardAddress": "address[0].add",
          "cardAddress1": "address[0].add2",
          "cardCity": "address[0].city",
          "name": "Photos[&1].no",
          "lastName": "Photos[&1].caption",
          "name1": "Photos[&1].no",
          "lastName1": "Photos[&1].caption"
        },
        "Photos": {
          "*": {
            "Id": "Photos[&1].no",
            "Caption": "Photos[&1].caption"
          }
        }
      }
    }
  }
]

EXPECTED OUTPUT:

{
  "tab": {
    "text": "123456789"
  },
  "address": [
    {
      "add": "UK",
      "add2": "US",
      "mk": "mk"
    }
  ],
  "Photos": [
    {
      "no": "RAM",
      "caption2": "ABU"
    },
    {
      "no": "RAM1",
      "caption2": "ABU1"
    },
    {
      "no": "327703",
      "caption2": "TEST>> photo 1"
    },
    {
      "no": "327704",
      "caption2": "TEST>> photo 2"
    },
    {
      "no": "327704",
      "caption2": "TEST>> photo 2"
    }
  ]
}

I am very new to jolt transform. Your help is highly appreciated. Thanks

CodePudding user response:

You can use two consecutive shift transformation specs, in the first determine the groupings as desired such as in the following

[
  {
    "operation": "shift",
    "spec": {
      "Entity": {
        "card": {
          "cardNo": "tab.text",
          "cardAddress": "address[0].add",
          "cardAddress1": "address[0].add2",
          "cardC*": "address[0].mk",
          "nam*": "Photos.no",
          "lastNam*": "Photos.caption2"
        },
        "Photos": {
          "*": {
            "Id": "Photos.no",
            "Caption": "Photos.caption2"
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": "&", // "else" case --> the arrrays/objects/attributes other than "Photos"
      "Photos": {
        "*": {
          "*": {
            "@": "&3[&1].&2"
          }
        }
      }
    }
  }
]
  • Related