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 Transform accept element or array of elements

The API that I’m using is returning an array of elements or a single element, see INPUT_1 and INPUT_2. I need a spec that could handle both inputs and return the same output, see below examples.

Is this something possible? Am I asking too much?

I have INPUT_1:

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

{
  "rating": [
    {
      "primary-value": 3,
      "id": 1
    },
    {
      "quality-value": 3,
      "id": 2
    }
  ]
}

SPEC:

[
  {
    "operation": "shift",
    "spec": {
      "rating": {
        "*": {
          "*": "out[&1].fields.&"
        }
      }
    }
  }
]

OUTPUT_1:

{
  "out": [
    {
      "fields": {
        "primary-value": 3,
        "id": 1
      }
    },
    {
      "fields": {
        "quality-value": 3,
        "id": 2
      }
    }
  ]
}

If I have:
INPUT_2:

{
  "rating": {
    "primary-value": 3,
    "id": 1
  }
}

SPEC_2:

[
  {
    "operation": "shift",
    "spec": {
      "rating": {
        "*": "out[0].fields.&"
      }
    }
  }
]

Desired OUTPUT_2:

{
  "out": [
    {
      "fields": {
        "primary-value": 3,
        "id": 1
      }
    }
  ]
}

>Solution :

You can manage it by applying toList function within a modify spec before using shift spec such as

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": "=toList"
    }
  },
  { // nests the inputs of type input_2
    // while it doesn't have any impact for the inputs of type input_2
    "operation": "shift",
    "spec": {
      "rating": {
        "*": {
          "*": "out[&1].fields.&"
        }
      }
    }
  }
]
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