Home > database >  Recursive jq with multiple branches
Recursive jq with multiple branches

Time:09-28

I'm stuck. How do I use jq to transform the following json:

{
  "sitemap": {
    "tree": {
      "node": [
        {
          "text": "Main",
          "blocks": {
            "block": [
              {
                "title": "Header"
              },
              {
                "title": "General"
              },
              {
                "title": "Message"
              },
              {
                "title": "Daily"
              },
              {
                "title": "Recognition"
              },
              {
                "title": "Policies"
              },
            ]
          },
          "children": {
            "node": [
              {
                "text": "Hub",
                "blocks": null,
                "children": {
                  "node": [
                    {
                      "text": "Handbook",
                      "blocks": null,
                      "children": null
                    },
                    {
                      "text": "Jobs",
                      "blocks": null,
                      "children": null
                    },
                    {
                      "text": "Perks",
                      "blocks": null,
                      "children": null
                    },
                    {
                      "text": "Benefits",
                      "blocks": null,
                      "children": null
                    }
                  ]
                }
              },
              {
                "text": "Resources",
                "blocks": null,
                "children": {
                  "node": [
                    {
                      "text": "Forms",
                      "blocks": null,
                      "children": null
                    },
                    {
                      "text": "Procedures",
                      "blocks": null,
                      "children": null
                    },
                    {
                      "text": "Training",
                      "blocks": null,
                      "children": null
                    },
                    {
                      "text": "Scripts",
                      "blocks": null,
                      "children": null
                    },
                    {
                      "text": "Marketing",
                      "blocks": null,
                      "children": {
                        "node": [
                          {
                            "text": "Logos",
                            "blocks": null,
                            "children": null
                          },
                          {
                            "text": "Brochures",
                            "blocks": null,
                            "children": null
                          },
                          {
                            "text": "Signage",
                            "blocks": null,
                            "children": null
                          }
                        ]
                      }
                    },
                    {
                      "text": "Tips & Tricks",
                      "blocks": null,
                      "children": null
                    }
                  ]
                }
              },
              {
                "text": "About",
                "blocks": null,
                "children": {
                  "node": [
                    {
                      "text": "Strategy",
                      "blocks": null,
                      "children": null
                    }
                  ]
                }
              }
            ]
          }
        },
        {
          "text": "Search",
          "blocks": null,
          "children": null
        },
        {
          "text": "Logins",
          "blocks": null,
          "children": {
            "node": [
              {
                "text": "One",
                "blocks": null,
                "children": null
              },
              {
                "text": "Two",
                "blocks": null,
                "children": null
              }
            ]
          }
        }
      ]
    }
  }
}

into a list of all pages – and with "blocks" contained in parenthesis.

[
   [ "Main" ],
   [ "Main", "(Header)" ],
   [ "Main", "(General)" ],
   [ "Main", "(Message)" ],
   [ "Main", "(Daily)" ],
   [ "Main", "(Recognition)" ],
   [ "Main", "(Policies)" ],
   [ "Main", "Hub" ],
   [ "Main", "Hub", "Handbook" ],
   [ "Main", "Hub", "Jobs" ],
   [ "Main", "Hub", "Perks" ],
   [ "Main", "Hub", "Benefits" ],
   [ "Main", "Resources" ],
   [ "Main", "Resources", "Forms" ],
   [ "Main", "Resources", "Procedures" ],
   [ "Main", "Resources", "Training" ],
   [ "Main", "Resources", "Scripts" ],
   [ "Main", "Resources", "Marketing" ],
   [ "Main", "Resources", "Marketing", "Logos" ],
   [ "Main", "Resources", "Marketing", "Brochures" ],
   [ "Main", "Resources", "Marketing", "Signage" ],
   [ "Main", "Resources", "Tips & Tricks" ],
   [ "Main", "About" ],
   [ "Main", "About", "Strategy" ],
   [ "Search" ],
   [ "Logins" ],
   [ "Logins", "One" ],
   [ "Logins", "Two" ]
]

CodePudding user response:

I figured it out, but if there's a better way let me know and I'll mark that as the accepted answer.

def b($path):
  [ $path[], "(\(.title?))" ]
  ;
def n($path):
  ($path   [.text]) as $path
  | $path, 
  if has ("blocks") then .blocks.block?[]? | b($path) else empty end,
  if has ("children") then .children.node?[]? | n($path) else empty end
  ;
.sitemap.tree.node[] | n([])

CodePudding user response:

Here's one way:

def nodes:
  .node[] | [.text]   (
    [],
    (.blocks.block[]? | ["(\(.title))"]),
    (.children | nodes?)
  );

[.sitemap.tree | nodes]
[
  ["Main"],
  ["Main","(Header)"],
  ["Main","(General)"],
  ["Main","(Message)"],
  ["Main","(Daily)"],
  ["Main","(Recognition)"],
  ["Main","(Policies)"],
  ["Main","Hub"],
  ["Main","Hub","Handbook"],
  ["Main","Hub","Jobs"],
  ["Main","Hub","Perks"],
  ["Main","Hub","Benefits"],
  ["Main","Resources"],
  ["Main","Resources","Forms"],
  ["Main","Resources","Procedures"],
  ["Main","Resources","Training"],
  ["Main","Resources","Scripts"],
  ["Main","Resources","Marketing"],
  ["Main","Resources","Marketing","Logos"],
  ["Main","Resources","Marketing","Brochures"],
  ["Main","Resources","Marketing","Signage"],
  ["Main","Resources","Tips & Tricks"],
  ["Main","About"],
  ["Main","About","Strategy"],
  ["Search"],
  ["Logins"],
  ["Logins","One"],
  ["Logins","Two"]
]

Demo

  • Related