Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Jolt Transformation – Move object to array

I’m coming to the conclusion that Jolt is beyond me.

With this input data:-

{
  "cluster_id": "1",
  "data": {
    "id": 1,
    "types": [
      {
        "incident_id": 10,
        "incident_ref": "AAA",
        "incident_code": "123",
        "incident_date": "2010-11-15T00:01:00Z"
      },
      {
        "incident_id": 20,
        "incident_ref": "BBB",
        "incident_code": "456",
        "incident_date": "2020-11-15T00:01:00Z"
      }
    ]
  }
}

Spec:-

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[
  {
    "operation": "shift",
    "spec": {
      "cluster_id": "id",
      "data": {
        "types": {
          "*": {
            "incident_id": "incidents",
            "incident_ref": "incidents"
          }
        }
      }
    }
  }
]

Gives:-

{
  "id" : "1",
  "incidents" : [ 10, "AAA", 20, "BBB" ]
}

How would I get the result of:-

{
  "id" : "1",
  "incidents" : [
    {"id": 10, "ref": "AAA", "code": "123", date: "2010-11-15T00:01:00Z"},
    {"id": 20, "ref": "BBB", "code": "456", date: "2020-11-15T00:01:00Z"},
  ]
}

Tried a bunch of permutations but getting nowhere!

>Solution :

You can use this spec:

[
  {
    "operation": "shift",
    "spec": {
      "*": "id",
      "data": {
        "types": {
          "*": {
            "incident_*": "incidents[&1].&(0,1)"
          }
        }
      }
    }
  }
]

To prevent using incident text in the spec, you can use the below spec:

[
  {
    "operation": "shift",
    "spec": {
      "*": "id",
      "data": {
        "types": {
          "*": {
            "*_*": "&(0,1)[&1].&(0,2)"
          }
        }
      }
    }
  }
]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading