1
votes

I just started learning jolt transformations.I found this scenario. The spec also is posted, but I am trying to understand the spec, and confused with the levels of the fields using "&". From the below spec: I am confused with this syntax "name": "state[&3].cities[&1].cityname". Why "name": "state[&2].cities[&1].cityname" doesn't give any output? As per my understanding the level of state for city name is &2. Can someone please try to explain why &3 is used.

    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"
                }
            ]
        }
    ]
}

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"
            }
          }
        }
      }
    }
  }
]
1

1 Answers

0
votes
    {
    "operation": "shift",
    "spec": {
      "country": "country",
      "state": {
        "*": { // state array index                             <- level 3
          "stateName": "state[&1].stateName",
          "location":  "state[&1].location",
          "cities": {                                           <- level 2  
            "*": { // city array index                          <- level 1
              "name": "state[&3].cities[&1].cityname",          <- level 0
              "pop":  "state[&3].cities[&1].citypopualtion"
            }
          }
        }
      }
    }
  }  

You are actually retrieving the index, that is represented by the "*". If you use &2 you are getting the cities and siblings level but you dont want to access those attributes. So during transformation it will actually end up being something like state[state_index].cities[city_index].cityname