使用JQ构建目录树的JSON

时间:2018-09-26 18:03:29

标签: json jq

我正在尝试使用jq构建目录树的JSON。我在其中途中,但目录树是平坦的,我需要将每个目录放入其父children数组中。

到目前为止,我的代码是dirtree.jq

[
    split("\n\n") as $list

    | $list
    | .[0]
    | split("\n")
    | .  + [  .[0] | split("\\") | .[0:-1] | join("\\")  ] # add root folder
    | sort
    | . as $folders

    | $list
    | .[1]
    | split("\n") as $files

    | $folders [] as $parent


    | {
        path: $parent,
        children: (
            $files
            | map({
                path: select(
                    . | (split("\\") | .[0:-1] | join("\\")) as $fileParent # get parent path
                      | $fileParent == $parent
                )
            })
        )
    }



] as $flatTree
| $flatTree

input文件是格式为原始的纯文本文件,格式为:

  • 首先是给定文件夹的所有目录

  • 然后一个空行

  • 然后所有文件的列表:


C:\Program Files\7-Zip
C:\Program Files\ASUS
C:\Program Files\Common Files
C:\Program Files\Intel
C:\Program Files\Internet Explorer
C:\Program Files\Mozilla Firefox
C:\Program Files\MSBuild
C:\Program Files\NVIDIA Corporation
C:\Program Files\Realtek
.
.
.
C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\1.0.0.1\en-US
C:\Program Files\WindowsPowerShell\Modules\PSReadline\1.2
C:\Program Files\WindowsPowerShell\Modules\PSReadline\1.2\en
                <-- NOTE THIS EMPTY LINE (\n\n) that separates folders from files
C:\Program Files\desktop.ini
C:\Program Files\7-Zip\7-zip.chm
C:\Program Files\7-Zip\7-zip.dll
C:\Program Files\7-Zip\7-zip32.dll
C:\Program Files\7-Zip\7z.dll
C:\Program Files\7-Zip\7z.exe
C:\Program Files\7-Zip\7z.sfx
C:\Program Files\7-Zip\7zCon.sfx
C:\Program Files\7-Zip\7zFM.exe
C:\Program Files\7-Zip\7zG.exe
C:\Program Files\7-Zip\descript.ion
.
.
.
C:\Program Files\WindowsPowerShell\Modules\PSReadline\1.2\Microsoft.PowerShell.PSReadline.dll
C:\Program Files\WindowsPowerShell\Modules\PSReadline\1.2\PSReadline.psd1
C:\Program Files\WindowsPowerShell\Modules\PSReadline\1.2\PSReadline.psm1
C:\Program Files\WindowsPowerShell\Modules\PSReadline\1.2\en\Microsoft.PowerShell.PSReadline.Resources.dll

resulting json已将所有文件添加到其各自的父级,但是现在我需要将每个文件夹移动到其parent的{​​{1}}数组:< / p>


enter image description here

命令行:

children

1 个答案:

答案 0 :(得分:1)

以下内容可能并不是您想要的,但应该会帮助您:

def parsePathname: split("\\") | {path: .[0:length-1], file: .[-1]};

# skip over lines that only specify directories
def pathnames:
  foreach inputs as $x (null;
    if . == null
    then if ($x|length) == 0 then 0 else . end
    else .+1
    end;
    select(. and . > 0)|$x)
    | parsePathname ;

reduce pathnames as $pn ({};
  getpath($pn.path + ["children"]) as $children
  | setpath($pn.path + ["children"]; $children + [$pn.file]) )

输出

将列表中的指示行修剪掉后,运行以下命令:

jq -R -n -f dirtree.jq dirtree.txt

产生:

{
  "C:": {
    "Program Files": {
      "children": [
        "desktop.ini"
      ],
      "7-Zip": {
        "children": [
          "7-zip.chm",
          "7-zip.dll",
          "7-zip32.dll",
          "7z.dll",
          "7z.exe",
          "7z.sfx",
          "7zCon.sfx",
          "7zFM.exe",
          "7zG.exe",
          "descript.ion"
        ]
      },
      "WindowsPowerShell": {
        "Modules": {
          "PSReadline": {
            "1.2": {
              "children": [
                "Microsoft.PowerShell.PSReadline.dll",
                "PSReadline.psd1",
                "PSReadline.psm1"
              ],
              "en": {
                "children": [
                  "Microsoft.PowerShell.PSReadline.Resources.dll"
                ]
              }
            }
          }
        }
      }
    }
  }
}