pramod wadappi
pramod wadappi

Reputation: 101

JOLT : Make Nested Make a nested array as part of main array

I am trying to transform the JSON as below expected output. Stuck with the spec below. Can someone help in this?

There is an inner array with name "content" in the "results" array which I want to make it as a part of main array.

Input JSON

{
  "total": 100,
  "start": 1,
  "page-length": 10,
  "results": [
    {
      "index": 1,
      "uri": "uri1/uri2",
      "extracted": {
        "kind": "object",
        "content": [
          {
            "code": "A1",
            "region": "APAC"
          }
        ]
      }
    },
    {
      "index": 2,
      "uri": "uri1/uri2",
      "extracted": {
        "kind": "object",
        "content": [
          {
            "code": "B1",
            "region": "AMER"
          }
        ]
      }
    },
    {
      "index": 3,
      "uri": "uri1/uri2",
      "extracted": {
        "kind": "object",
        "content": [
          {
            "code": "C1",
            "region": "APAC"
          }
        ]
      }
    }
  ]
}

Expected json output

[
  {
    "code": "A1",
    "region": "APAC"
  },
  {
    "code": "B1",
    "region": "AMER"
  },
  {
    "code": "C1",
    "region": "APAC"
  }
]

Spec Tried

[
  {
    "operation": "shift",
    "spec": {
      "results": {
        "*": {
          "extracted": { "content": { "@": "&" } }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "content": {
        "@": "&"
      }
    }
  }

]

Find the output below I am getting on Jolt tool enter image description here

Upvotes: 2

Views: 40

Answers (1)

Barbaros Özhan
Barbaros Özhan

Reputation: 65408

You can use # wildcard nested within square brackets in order to reach the level of the indexes of the "results" array such that

[
  {
    "operation": "shift",
    "spec": {
      "results": {
        "*": {//the indexes of the "results" array
          "extracted": {
            "content": {
              "*": {//the indexes of the "content" array
                "*": "[#5].&"
              }
            }
          }
        }
      }
    }
  }
]

Also the following spec, which repeats the content of the inner array without keys, will give the same result :

[
  {
    "operation": "shift",
    "spec": {
      "results": {
        "*": {
          "extracted": {
            "content": {
              "*": "[]"// [] seems like redundant but kept for the case the array has a single object.
            }
          }
        }
      }
    }
  }
]

which is quite similar to your tried one.

Upvotes: 2

Related Questions