Search code examples
jsontransformjolt

Can't extract a value inside nested JSON using Jolt Transform


I have this problem.

Given this sample JSON

[
  {
    "aaa": "-",
    "name": "something_val",
    "bbb": "-",
    "is_active": true,
    "repository": null,
    "image_url": "some_val",
    "ccc_id_tmp": 2,
    "ddd_id_tmp": 3,
    "ccc_created_at": "timestamp",
    "updated_at": "timestamp",
    "result_ccc_id_lookup": {
      "ccc_id": "some_uuid"
    },
    "result_ddd_id_lookup": {
      "ddd_id": "some_uuid"
    }
  },
  {
    "aaa": "hi",
    "name": "something_val",
    "bbb": "yooo",
    "is_active": false,
    "repository": null,
    "image_url": "some_val",
    "ccc_id_tmp": 4,
    "ddd_id_tmp": 5,
    "ccc_created_at": "timestamp",
    "updated_at": "timestamp",
    "result_ccc_id_lookup": {
      "ccc_id": "some_uuid_2"
    },
    "result_ddd_id_lookup": {
      "ddd_id": "some_uuid_2"
    }
  }
]

The goal is I want to use Jolt Transform to extract the values inside result_ccc_id_lookup and result_ddd_id_lookup, meaning I will extract the ccc_id and ddd_id values.

So, the expected output will be like this

{
  "aaa": "-",
  "name": "something_val",
  "bbb": "-",
  "is_active": true,
  "repository": null,
  "image_url": "some_val",
  "ccc_id_tmp": 2,
  "ddd_id_tmp": 3,
  "ccc_created_at": "timestamp",
  "updated_at": "timestamp",
  "ccc_id": "some_uuid",
  "ddd_id": "some_uuid"
}

Right now, I'm using this Jolt Transform to extract the value like the expected output

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "aaa": "[&1].aaa",
        "bbb": "[&1].bbb",
        "objective": "[&1].objective",
        "is_active": "[&1].is_active",
        "repository": "[&1].repository",
        "image_url": "[&1].image_url",
        "ccc_id_tmp": "[&1].ccc_id_tmp",
        "ddd_id_tmp": "[&1].ddd_id_tmp",
        "ccc_created_at": "[&1].ccc_created_at",
        "updated_at": "[&1].updated_at",
        "result_ccc_id_lookup": {
          "ccc_id": "[&1].ccc_id"
        },
        "result_ddd_id_lookup": {
          "ddd_id": "[&1].ddd_id"
        }
      }
    }
  }
]

But, the output is only like this. The ccc_id and ddd_id are missing

{
  "aaa": "-",
  "name": "something_val",
  "bbb": "-",
  "is_active": true,
  "repository": null,
  "image_url": "some_val",
  "ccc_id_tmp": 2,
  "ddd_id_tmp": 3
}

Can you help me with this one? maybe something is wrong with my Jolt Transform?

Thank you


Solution

  • No need to articulate every attribute one by one, but the following transformation is enough to express all

    [
      {
        "operation": "shift",
        "spec": {
          "*": {
            "*": "[&1].&",  // replicate all attributes those have the key which don't start with "result" 
            "result*": {    // other elements taken from the deeper level of one step
              "*": "[&2].&" // so increment the degree of identifier by 1, eg. [&1]->[&2]
            }
          }
        }
      }
    ]
    

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

    enter image description here