Python에서 JSON 데이터 처리 중
글쓴이: mandugukbap / 작성시간: 금, 2022/04/08 - 5:18오후
아래의 json 데이터(test.json)를 그 아래에 있는 python 코드로 읽고 파싱하려고 합니다.
다른 element들을 잘 읽어지는데 pkts_json["_source"]["layers"]["dccp"]["dccp.options"] 레벨의 하위 element들이 다 읽혀지지 않고 마지막 element 하나만 읽혀 집니다.
즉, 아래 데이터에서 추출하고 싶은 것은 "dccp.option_reserved": "04:00:00:00:00:05:62" <- 이 라인인데 도무지 파싱할 수가 없습니다. Python 쌩초보인 제가 뭔가를 놓치고 있는건지 가르쳐 주세요.
감사합니다.
파일: test.json
[
{
"_index": "packets-2022-03-30",
"_type": "doc",
"_score": null,
"_source": {
"layers": {
"frame": {
"frame.encap_type": "1",
"frame.time": "Mar 30, 2022 11:29:16.414680000 CEST",
"frame.offset_shift": "0.000000000",
"frame.time_epoch": "1648632556.414680000",
"frame.time_delta": "0.340680000",
"frame.time_delta_displayed": "0.000000000",
"frame.time_relative": "0.505711000",
"frame.number": "7",
"frame.len": "145",
"frame.cap_len": "145",
"frame.marked": "0",
"frame.ignored": "0",
"frame.protocols": "eth:ethertype:ip:dccp:data"
},
"eth": {
"eth.dst": "1c:69:7a:63:bb:1f",
"eth.dst_tree": {
"eth.dst_resolved": "EliteGro_63:bb:1f",
"eth.dst.oui": "1862010",
"eth.dst.oui_resolved": "EliteGroup Computer Systems Co., LTD",
"eth.addr": "1c:69:7a:63:bb:1f",
"eth.addr_resolved": "EliteGro_63:bb:1f",
"eth.addr.oui": "1862010",
"eth.addr.oui_resolved": "EliteGroup Computer Systems Co., LTD",
"eth.dst.lg": "0",
"eth.lg": "0",
"eth.dst.ig": "0",
"eth.ig": "0"
},
"eth.src": "74:42:7f:05:ad:7a",
"eth.src_tree": {
"eth.src_resolved": "74:42:7f:05:ad:7a",
"eth.src.oui": "7619199",
"eth.addr": "74:42:7f:05:ad:7a",
"eth.addr_resolved": "74:42:7f:05:ad:7a",
"eth.addr.oui": "7619199",
"eth.src.lg": "0",
"eth.lg": "0",
"eth.src.ig": "0",
"eth.ig": "0"
},
"eth.type": "0x00000800"
},
"ip": {
"ip.version": "4",
"ip.hdr_len": "20",
"ip.dsfield": "0x00000000",
"ip.dsfield_tree": {
"ip.dsfield.dscp": "0",
"ip.dsfield.ecn": "0"
},
"ip.len": "131",
"ip.id": "0x00006cae",
"ip.flags": "0x00004000",
"ip.flags_tree": {
"ip.flags.rb": "0",
"ip.flags.df": "1",
"ip.flags.mf": "0"
},
"ip.frag_offset": "0",
"ip.ttl": "63",
"ip.proto": "33",
"ip.checksum": "0x00004b17",
"ip.checksum.status": "2",
"ip.src": "192.168.1.56",
"ip.addr": "192.168.1.56",
"ip.src_host": "192.168.1.56",
"ip.host": "192.168.1.56",
"ip.dst": "192.168.1.12",
"ip.addr": "192.168.1.12",
"ip.dst_host": "192.168.1.12",
"ip.host": "192.168.1.12"
},
"dccp": {
"dccp.srcport": "43343",
"dccp.port": "43343",
"dccp.dstport": "1707",
"dccp.port": "1707",
"dccp.data_offset": "11",
"dccp.ccval": "0",
"dccp.cscov": "0",
"dccp.checksum": "0x00007b6d",
"dccp.checksum.status": "1",
"dccp.res1": "0x00000000",
"dccp.type": "4",
"dccp.x": "1",
"dccp.res2": "0x00000000",
"dccp.seq": "148465425745455",
"dccp.ack_res": "0x00000000",
"dccp.ack": "125202981029830",
"dccp.options": {
"dccp.option_type": "0",
"dccp.option_type_tree": {
"dccp.padding": "00"
},
"dccp.option_type": "45",
"dccp.option_type_tree": {
"dccp.option_reserved": "0a:00:00:00:04"
},
"dccp.option_type": "45",
"dccp.option_type_tree": {
"dccp.option_reserved": "04:00:00:00:00:05:62"
},
"dccp.option_type": "38",
"dccp.option_type_tree": {
"dccp.ack_vector.nonce_0": "01"
}
}
},
"data": {
"data.data": "45:00:00:43:f6:51:40:00:40:11:78:92:0a:04:00:10:c0:a8:01:0a:a5:35:00:35:00:2f:ce:d9:aa:97:01:00:00:01:00:00:00:00:00:00:01:30:06:64:65:62:69:61:6e:04:70:6f:6f:6c:03:6e:74:70:03:6f:72:67:00:00:01:00:01",
"data.len": "67"
}
}
}
}
]파일: try.py
import json f = open("test.json", "r") pkts = f.read() pkts_json = json.loads(pkts) print(json.dumps(pkts_json[0]["_source"]["layers"]["dccp"]["dccp.options"], indent=2))
File attachments:
| 첨부 | 파일 크기 |
|---|---|
| 4.11 KB | |
| 270바이트 |
Forums:


매뉴얼! 매뉴얼을 보시오!
https://docs.python.org/3/library/json.html#repeated-names-within-an-object
JSON 생성이 잘못되었어요. object 내에 같은
JSON 생성이 잘못되었어요. object 내에 같은 key 값이 중복으로 있으면 안됩니다.
dccp.options 하위에 dccp.option_type나 dccp.option_type_tree 등이 한 번씩만 있어야 됩니다.
Wireshark에서 export한 json 파일인데
wireshark가 문제가 있군요. 친절하고 문명화된 댓글 감사합니다.
댓글 달기