我有一个JSON对象数组。示例数组如下所示:
[
{
"evt.category": "file",
"evt.cpu": 0,
"evt.num": 10078507,
"evt.res": "SUCCESS",
"evt.time": 1532841047277584400,
"evt.type": "read",
"fd.filename": "libnss_files.so.2",
"fd.name": "/lib/x86_64-linux-gnu/libnss_files.so.2",
"fd.num": 13,
"fd.type": "file",
"fd.uid": "1996913",
"proc.loginshellid": 19968,
"proc.name": "last",
"proc.pid": 19969,
"thread.ismain": true,
"thread.tid": 19969
},
{
"evt.buffer": "1000",
"evt.category": "file",
"evt.cpu": 0,
"evt.num": 10078564,
"evt.res": "SUCCESS",
"evt.time": 1532841047277731300,
"evt.type": "read",
"fd.filename": "loginuid",
"fd.name": "/proc/16009/loginuid",
"fd.num": 13,
"fd.type": "file",
"fd.uid": "1996913",
"proc.loginshellid": 19968,
"proc.name": "last",
"proc.pid": 19969,
"thread.ismain": true,
"thread.tid": 19969
},
{
"evt.buffer": "",
"evt.category": "file",
"evt.cpu": 0,
"evt.num": 10078566,
"evt.res": "SUCCESS",
"evt.time": 1532841047277733400,
"evt.type": "read",
"fd.filename": "loginuid",
"fd.name": "/proc/16009/loginuid",
"fd.num": 13,
"fd.type": "file",
"fd.uid": "1996913",
"proc.loginshellid": 19968,
"proc.name": "last",
"proc.pid": 19969,
"thread.ismain": true,
"thread.tid": 19969
}
]
我想重新构造这个数组,使每个对象都转换成另一个数组,并且每个数组应该包含基于这些键的JSON对象,比如evt
、proc
thread
等JSON对象
我尝试了一些在线网站,但都没有成功。你知道吗
请帮忙。你知道吗
编辑: 我期望的输出如下:
[
{
"evt": {
"category": "file",
"cpu": 0,
"num": 10078507,
"res": "SUCCESS",
"time": 1532841047277584400,
"type": "read"
},
"fd": {
"filename": "libnss_files.so.2",
"name": "/lib/x86_64-linux-gnu/libnss_files.so.2",
"num": 13,
"type": "file",
"uid": "1996913"
},
"proc": {
"loginshellid": 19968,
"name": "last",
"pid": 19969
},
"thread": {
"ismain": true,
"tid": 19969
}
},
{
"evt": {
"buffer": "1000",
"category": "file",
"cpu": 0,
"num": 10078564,
"res": "SUCCESS",
"time": 1532841047277731300,
"type": "read"
},
"fd": {
"filename": "loginuid",
"name": "/proc/16009/loginuid",
"num": 13,
"type": "file",
"uid": "1996913"
},
"proc": {
"loginshellid": 19968,
"name": "last",
"pid": 19969
},
"thread" : {
"ismain": true,
"tid": 19969
}
}
]
使用jq可以很容易地完成任务,例如使用jq可执行文件、pythonjq模块或pythonpyjq模块。你知道吗
无论您选择哪种模式,合适的过滤器都是:
示例(使用jq可执行文件)
对于示例输入,调用:
收益率:
粗壮化
以上假设所有键名至少有一个“.”。如果不能假定这一点,那么可以使用
restructure
的以下变体:var obj = [{ "evt.category": "file", "evt.cpu": 0, "evt.num": 10078507, "evt.res": "SUCCESS", "evt.time": 1532841047277584400, "evt.type": "read", "fd.filename": "libnss_files.so.2", "fd.name": "/lib/x86_64-linux-gnu/libnss_files.so.2", "fd.num": 13, "fd.type": "file", "fd.uid": "1996913", "proc.loginshellid": 19968, "proc.name": "last", "proc.pid": 19969, "thread.ismain": true, "thread.tid": 19969 }, { "evt.buffer": "1000", "evt.category": "file", "evt.cpu": 0, "evt.num": 10078564, "evt.res": "SUCCESS", "evt.time": 1532841047277731300, "evt.type": "read", "fd.filename": "loginuid", "fd.name": "/proc/16009/loginuid", "fd.num": 13, "fd.type": "file", "fd.uid": "1996913", "proc.loginshellid": 19968, "proc.name": "last", "proc.pid": 19969, "thread.ismain": true, "thread.tid": 19969 }, { "evt.buffer": "", "evt.category": "file", "evt.cpu": 0, "evt.num": 10078566, "evt.res": "SUCCESS", "evt.time": 1532841047277733400, "evt.type": "read", "fd.filename": "loginuid", "fd.name": "/proc/16009/loginuid", "fd.num": 13, "fd.type": "file", "fd.uid": "1996913", "proc.loginshellid": 19968, "proc.name": "last", "proc.pid": 19969, "thread.ismain": true, "thread.tid": 19969 } ]; function convertObj(obj) { var resultArray = []; obj.forEach(item => { var resultObj = {}; for (var property in item) { var array = property.split('.'); var reference = resultObj; for (var i = 0; i < array.length-1; i++) { if (!reference[array[i]]){ reference[array[i]] = {}; } reference = reference[array[i]]; } reference[array[array.length-1]] = item[property]; } resultArray.push(resultObj); }); return resultArray; } console.log(convertObj(obj));
;相关问题 更多 >
编程相关推荐