2

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

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.