基于Python关键代码将JSON对象转换为JSON对象数组

2024-09-29 22:32:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个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对象,比如evtprocthread等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
    }
  }
]

Tags: nametimetyperes数组proccputhread
3条回答

使用可以很容易地完成任务,例如使用jq可执行文件、pythonjq模块或pythonpyjq模块。你知道吗

无论您选择哪种模式,合适的过滤器都是:

def restructure:
  def splitup: index(".") as $ix | [ .[0:$ix], .[1+$ix:] ];
  to_entries
  | map( (.key|splitup) + [.value] )
  | reduce .[] as $x ({}; .[$x[0]][$x[1]] = $x[2]) ;

map(restructure)

示例(使用jq可执行文件)

对于示例输入,调用:

jq -f program.jq input.jq

收益率:

[
  {
    "evt": {
      "category": "file",
      "cpu": 0,
      "num": 10078507,
      "res": "SUCCESS",
      "time": 1532841047277584400,
      "type": "read"
    },
    "fd": {
      "num": 13,
      "name": "/lib/x86_64-linux-gnu/libnss_files.so.2",
      "filename": "libnss_files.so.2",
      "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": {
      "name": "/proc/16009/loginuid",
      "filename": "loginuid",
      "num": 13,
      "type": "file",
      "uid": "1996913"
    },
    "proc": {
      "loginshellid": 19968,
      "name": "last",
      "pid": 19969
    },
    "thread": {
      "ismain": true,
      "tid": 19969
    }
  },
  {
    "evt": {
      "buffer": "",
      "category": "file",
      "cpu": 0,
      "num": 10078566,
      "res": "SUCCESS",
      "time": 1532841047277733400,
      "type": "read"
    },
    "fd": {
      "name": "/proc/16009/loginuid",
      "filename": "loginuid",
      "num": 13,
      "type": "file",
      "uid": "1996913"
    },
    "proc": {
      "loginshellid": 19968,
      "name": "last",
      "pid": 19969
    },
    "thread": {
      "ismain": true,
      "tid": 19969
    }
  }
]

粗壮化

以上假设所有键名至少有一个“.”。如果不能假定这一点,那么可以使用restructure的以下变体:

def restructure:
  def splitup: index(".") as $ix
    | if $ix then [ .[0:$ix], .[1+$ix:] ]
      else [ ., null] 
      end;
  to_entries
  | map( (.key|splitup) + [.value] )
  | reduce .[] as $x ({};
      if $x[1] == null then .[$x[0]] = $x[2]
      else .[$x[0]][$x[1]] = $x[2]
      end ) ;
import json

list_of_objects = json.loads(json_string)

new_list = []
for complex_object in list_of_objects:
    new_object = {}
    for composite_key, value in complex_object.items():
        key, subkey = composite_key.split(".")
        if key not in new_object:
            new_object[key] = {}
        new_object[key][subkey] = value
    new_list.append(new_object)

json_string = json.dumps(new_list)

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));

相关问题 更多 >

    热门问题