0
[
  {
    "path": "test",
    "resources": [
      {
        "name": "testfile"
      }
    ]
  },
  {
    "path": "test-1",
    "resources": [
      {
        "name": "testfile-1"
      }
    ]
  }
]

Given a json like above, is it possible to read it in following format using jsonPath?

[
  {
    "path": "test",
    "name": "testfile"
  },
  {
    "path": "test-1",
    "name": "testfile-1"
  }
]

Its safe to assume that resources array will always be size 1.

I tried $.[*]['path', ['resources'][0]['name']] but it does not show the value of path.

varun
  • 684
  • 1
  • 11
  • 30
  • I don't think `JsonPath` can achieve this, you need a JSON transformer such as `Jolt` instead. – LHCHIN Oct 16 '20 at 00:33

1 Answers1

0

As mentioned, this cannot be done with JSONPath; selecting items from different levels and recombining them into new array elements does not work, you need a JSON transformer like Jolt instead.

There are different ways but you could use shift transformer like shown below to get the desired output:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        // Grab the value of clientName, and write it to
        //  bookMap. whatever is at clientId
        "@path": "[&].path",
        "@resources[0].name": "[&].name"
      }
    }
  }
]

Try it online with your own input here.

wp78de
  • 18,207
  • 7
  • 43
  • 71