Python에서 JSON 데이터 처리 중

mandugukbap의 이미지

아래의 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: 
첨부파일 크기
Plain text icon test.json_.txt4.11 KB
Plain text icon try.py_.txt270바이트
익명 사용자의 이미지

peecky의 이미지

JSON 생성이 잘못되었어요. object 내에 같은 key 값이 중복으로 있으면 안됩니다.
dccp.options 하위에 dccp.option_type나 dccp.option_type_tree 등이 한 번씩만 있어야 됩니다.

mandugukbap의 이미지

wireshark가 문제가 있군요. 친절하고 문명화된 댓글 감사합니다.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.