glom icon indicating copy to clipboard operation
glom copied to clipboard

how can I find item in an deeply nested data struct?

Open rhinoceros opened this issue 6 years ago • 1 comments

how could I find branch item by name (may be name='branch-a-a-a' , or maybe name='branch-b' ) from a deeply (may be depth > 10) nested json? Can anyone help me ? thanks.

{
  "root": [
    {
      "type": "branch",
      "name": "branch-a",
      "children": [
        {
          "type": "branch",
          "name": "branch-a-a",
          "children": [
            {
              "type": "branch",
              "name": "branch-a-a-a",
              "children": [
                {
                  "type": "leaf",
                  "name": "leaf-a"
                },
                {
                  "type": "leaf",
                  "name": "leaf-aa"
                }
              ]
            }
          ]
        }
      ]
    },
    {
      "type": "branch",
      "name": "branch-b",
      "children": [
        {
          "type": "leaf",
          "name": "leaf-ba"
        },
        {
          "type": "leaf",
          "name": "leaf-bb"
        }
      ]
    }
  ]
}

rhinoceros avatar Jul 04 '18 04:07 rhinoceros

Hey @rhinoceros! glom is primarily for when you know the structure of your input and output data. When you need something more dynamic, I've written a separate tool, called remap:

I'd use the visit function to check for a value and then capture the path and key to get your answer.

mahmoud avatar Jul 04 '18 04:07 mahmoud