bazaarvoice / jolt

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

Unable to get expected output using JOLT parser #981

Open debuggerrr opened 4 years ago

debuggerrr commented 4 years ago

I have below input:

[
  {
    "ownerId": "XTV7I728",
    "vehicleId": "999",
    "engines": [
      {
        "engineId": "1",
        "engineName": "Standard Engine",
        "engineHp": 300
      },
      {
        "engineId": "2",
        "engineName": "Custom Engine",
        "engineHp": 450
      }
    ]
  }
]

I need the expected output as below:

[{
  "ownerId": "XTV7I728",
  "vehicleId": "999",
  "engineId": "1",
  "engineName": "Standard Engine",
  "engineHp": 300
},
{
  "ownerId": "XTV7I728",
  "vehicleId": "999",
  "engineId": "2",
  "engineName": "Custom Engine",
  "engineHp": 450
}]  

I tried below spec but it doesn't give the expected output:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "ownerId": "ownerId",
        "vehicleId": "vehicleId",
        "spec": {
          "engines": {
            "*": "[]"
          }
        }
      }
    }
  }
]

Output:

{
  "ownerId" : "XTV7I728",
  "vehicleId" : "999"
}

Please let me know where I went wrong.

loveall commented 3 years ago

spec can't have spec , i think below spec might helpful for you.

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "engines": {
          "@": "&",
          "*": {
            "@(2,ownerId)": "engines[&].ownerId",
            "@(2,vehicleId)": "engines[&].vehicleId"
          }
        }
      }
    }
  }
]