3

I have a nested JSON in a field that contains multiple important keys that I would like to retrieve as an array:

{
  "tasks": [
    {
      "id": "task_1",
      "name": "task_1_name",
      "assignees": [
        {
          "id": "assignee_1",
          "name": "assignee_1_name"
        }
      ]
    },
    {
      "id": "task_2",
      "name": "task_2_name",
      "assignees": [
        {
          "id": "assignee_2",
          "name": "assignee_2_name"
        },
        {
          "id": "assignee_3",
          "name": "assignee_3_name"
        }
      ]}]}

All the queries that I've tried so far fx ( $.tasks.*.assignees..id) and many others have returned

[
  "assignee_1",
  "assignee_2",
  "assignee_3"
]

But what I need is:

[
  ["assignee_1"],
  ["assignee_2", "assignee_3"]
]

Is it possible to do with JSONPath or any script inside of it, without involving 3rd party tools?

7
  • 1
    The basic issue is that JSONPath is a selection language. It can select any of the items that are in your JSON, and it will give you a list of matches, such as ["assignee_1", "assignee_2", "assignee_3"]. The result you want is not a selection, but a transformation - practically mapping "task" objects to arrays (root.tasks.map(t => t.assignees.map(a => a.id)). This is not something that JSONPath has been designed to do. Commented Apr 21, 2022 at 8:38
  • Similar question over here: stackoverflow.com/questions/30221930/… Commented Apr 21, 2022 at 8:39
  • That's a pity. Thank you for clarifying things @Tomalak ! Commented Apr 21, 2022 at 11:27
  • What environment are you in? Commented Apr 21, 2022 at 12:27
  • I'm using Pentaho Data Integrator with Json Input step Commented Apr 21, 2022 at 12:34

1 Answer 1

3

The problem you're facing is that tasks and assignees are arrays. You need to use [*] instead of .* to get the items in the array. So your path should look like

$.tasks[*].assignees[*].id

You can try it at https://json-everything.net/json-path.

NOTE The output from my site will give you both the value and its location within the original document.

Edit

(I didn't read the whole thing :) )

You're not going to be able to get

[
  ["assignee_1"],
  ["assignee_2", "assignee_3"]
]

because, as @Tomalak mentioned, JSON Path is a query language. It's going to remove all structure and return only values.

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

2 Comments

As of 2023-10-24, json-everything.net/json-path is displaying a spinning 3d rendering for me.
That's the just the Blazor initialization screen. Happy for you to open an issue at github.com/gregsdennis/json-everything with the browser, etc, that you're using.

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.