I want to convert the NCDU json file (https://dev.yorhel.nl/ncdu/jsonfmt) to a hierarchical json that can be imported in D3.js using D3.json()
. Here is the D3 sample file flare.json (https://raw.githubusercontent.com/d3/d3-hierarchy/main/test/data/flare.json).
Tree structure where NCDU is run:
dotfiles/
├── apache
│ └── security.conf
├── bash
│ └── bin
│ ├── ipChangeAlert.sh
│ ├── mycommands.sh
│ └── send_reboot_email.sh
└── settingUpGit.md
NCDU output data.json:
[1,1,{"progname":"ncdu","progver":"1.15.1","timestamp":1717703526},
[{"name":"dotfiles","asize":4096,"ino":6192449488235659},
[{"name":"apache","asize":4096,"ino":1970324837438018},
{"name":"security.conf","asize":97,"ino":2251799814148886}],
[{"name":"bash","asize":4096,"ino":2251799814286493},
[{"name":"bin","asize":4096,"ino":1970324837438020},
{"name":"ipChangeAlert.sh","asize":1187,"dsize":4096,"ino":1970324837438249},
{"name":"mycommands.sh","asize":2421,"dsize":4096,"ino":2251799814148893},
{"name":"send_reboot_email.sh","asize":356,"ino":1970324837438244}]],
[{"name":"htop","asize":4096,"ino":1407374884154553}],
{"name":"settingUpGit.md","asize":2504,"dsize":4096,"ino":1970324837438080}
]]
Desired output:
{
"name": "dotfiles",
"asize": 4096,
"dsize": 0,
"ino": 6192449488235659,
"hlnkc": false,
"read_error": false,
"nlink": 1,
"notreg": false,
"extension": "",
"children": [
{
"name": "apache",
"asize": 4096,
"dsize": 0,
"ino": 1970324837438018,
"hlnkc": false,
"read_error": false,
"nlink": 1,
"notreg": false,
"extension": "",
"children": [
{
"name": "security.conf",
"asize": 97,
"dsize": 0,
"ino": 2251799814148886,
"hlnkc": false,
"read_error": false,
"nlink": 1,
"notreg": false,
"extension": "conf"
}
]
}
]
}
I have tried using both js
in bash and iterating through the list in python, but neither approach seems to be managing the structure correctly, and I am getting directories that are dropped or files that are considered directories