3

I want to rename fields in an array nested in an another array using JOLT transformation library.

  1. One field to rename is a top level field in an array
  2. Two fields to rename are inside a nested array

I have tried using wildcards but they are not giving me expected output. I am using JOLT 0.0.22 version.

Input JSON:

{
  "country": "usa",
  "state": [
    {
      "stateName": "TX",
      "location": "south",
      "cities": [
        {
          "name": "Austin",
          "pop": "1M"
        },
        {
          "name": "Dallas",
          "pop": "2M"
        }
      ]
    },
    {
      "stateName": "CA",
      "location": "west",
      "cities": [
        {
          "name": "SanFran",
          "pop": "3M"
        },
        {
          "name": "LosAngeles",
          "pop": "4M"
        }
      ]
    }
  ]
}

Expected Output :

{
  "country": "usa",
  "state": [
    {
      "stateName": "TX",
      "locatedIn": "south", // name change here
      "cities": [
        {
          "cityname": "Austin", // name change here
          "citypopulation": "1M" // name change here
        },
        {
          "cityname": "Dallas",
          "citypopulation": "2M"
        }
      ]
    },
    {
      "stateName": "CA",
      "locatedIn": "west",
      "cities": [
        {
          "cityname": "SanFran",
          "pop": "3M"
        },
        {
          "cityname": "LosAngeles",
          "citypopulation": "4M"
        }
      ]
    }
  ]
}
Barbaros Özhan
  • 59,113
  • 10
  • 31
  • 55
abb
  • 352
  • 1
  • 10
  • 23

3 Answers3

8

Spec

[
  {
    "operation": "shift",
    "spec": {
      "country": "country",
      "state": {
        "*": { // state array index
          "stateName": "state[&1].stateName",
          "location":  "state[&1].location",
          "cities": {
            "*": { // city array index
              "name": "state[&3].cities[&1].cityname",
              "pop":  "state[&3].cities[&1].citypopualtion"
            }
          }
        }
      }
    }
  }
]
Milo S
  • 4,466
  • 1
  • 19
  • 22
  • Could you please explain what that spec means? – Pokuri Apr 24 '19 at 19:31
  • @Pokuri Have a look at the Java Doc https://github.com/bazaarvoice/jolt/blob/04251c6111f5ee3fa65f6c353dba89e08c796c0a/jolt-core/src/main/java/com/bazaarvoice/jolt/Shiftr.java#L184 – DarkLeafyGreen Aug 05 '19 at 07:43
3

For comparison, this is the solution in JSLT.

{
  "state" : [for (.state) . | {
    "locatedIn" : .location,
    "cities" : [for (.cities) {
      "cityname" : .name,
      "citypopulation" : .pop
    }],
    * : .
  }],
  * : .
}
0

Currently as an option, you could generate new keys by copying the existing ones through use of a modify transformation, and the get rid of the originals by using remove transformation spec such as

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "state": {
        "*": {
          "locatedIn": "@1,location",
          "cities": {
            "*": {
              "cityname": "@1,name",
              "citypopulation": "@1,pop"
            }
          }
        }
      }
    }
  },
  {
    "operation": "remove",
    "spec": {
      "*": {
        "*": {
          "location": "",
          "*": {
            "*": {
              "name|pop": ""
            }
          }
        }
      }
    }
  }
]

the demo on the site http://jolt-demo.appspot.com/ is

enter image description here

Barbaros Özhan
  • 59,113
  • 10
  • 31
  • 55