-1

I have an extra last square } in a big json file, I need to remove it by using python :

{
        "layers": {
        "frame": {
          "frame.interface_id": "0",
          "frame.encap_type": "127",
          "frame.time": "Oct 10, 2017 18:05:51.620568000 Central European Daylight Time",
          "frame.offset_shift": "0.000000000",
          "frame.time_epoch": "1507651551.620568000",
          "frame.time_delta": "0.324011000",
          "frame.time_delta_displayed": "0.324011000",
          "frame.time_relative": "29.248970000",
          "frame.number": "38",
          "frame.len": "64",
          "frame.cap_len": "64",
          "frame.marked": "0",
          "frame.ignored": "0",
          "frame.protocols": "wpan:6lowpan:ipv6:ipv6.hopopts:udp:data",
          "frame.coloring_rule.name": "UDP",
          "frame.coloring_rule.string": "udp"
        },
        "wpan": {
          "wpan.frame_length": "66",
          "wpan.fcf": "0x0000dc41",
          "wpan.fcf_tree": {
            "wpan.frame_type": "0x00000001",
            "wpan.security": "0",
            "wpan.pending": "0",
            "wpan.ack_request": "0",
            "wpan.pan_id_compression": "1",
            "wpan.seqno_suppression": "0",
            "wpan.ie_present": "0",
            "wpan.dst_addr_mode": "0x00000003",
            "wpan.version": "1",
            "wpan.src_addr_mode": "0x00000003"
          },
          "wpan.seq_no": "8",
          "wpan.dst_pan": "0x0000abcd",
          "wpan.dst64": "00:21:2f:3c:c6:b5:00:01",
          "wpan.src64": "00:21:2f:3c:c6:b5:00:7e",
          "wpan.fcs_ok": "1"
        },
        "6lowpan": {
          "IPHC Header": {
            "6lowpan.pattern": "0x00000003",
            "6lowpan.iphc.tf": "0x00000003",
            "6lowpan.iphc.nh": "0",
            "6lowpan.iphc.hlim": "0x00000002",
            "6lowpan.iphc.cid": "1",
            "6lowpan.iphc.sac": "1",
            "6lowpan.iphc.sam": "0x00000003",
            "6lowpan.iphc.m": "0",
            "6lowpan.iphc.dac": "1",
            "6lowpan.iphc.dam": "0x00000003",
            "6lowpan.iphc.sci": "0x00000000",
            "6lowpan.iphc.dci": "0x00000000"
          },
          "6lowpan.next": "0x00000000",
          "6lowpan.src": "::221:2f3c:c6b5:7e",
          "6lowpan.dst": "::221:2f3c:c6b5:1"
        },
        "ipv6": {
          "ipv6.version": "6",
          "ip.version": "6",
          "ipv6.tclass": "0x00000000",
          "ipv6.tclass_tree": {
            "ipv6.tclass.dscp": "0",
            "ipv6.tclass.ecn": "0"
          },
          "ipv6.flow": "0x00000000",
          "ipv6.plen": "39",
          "ipv6.nxt": "0",
          "ipv6.hlim": "64",
          "ipv6.src": "::221:2f3c:c6b5:7e",
          "ipv6.addr": "::221:2f3c:c6b5:7e",
          "ipv6.src_host": "::221:2f3c:c6b5:7e",
          "ipv6.host": "::221:2f3c:c6b5:7e",
          "ipv6.dst": "::221:2f3c:c6b5:1",
          "ipv6.addr": "::221:2f3c:c6b5:1",
          "ipv6.dst_host": "::221:2f3c:c6b5:1",
          "ipv6.host": "::221:2f3c:c6b5:1",
          "Source GeoIP: Unknown": "",
          "Destination GeoIP: Unknown": "",
          "ipv6.hopopts": {
            "ipv6.hopopts.nxt": "17",
            "ipv6.hopopts.len": "0",
            "ipv6.hopopts.len_oct": "8",
            "ipv6.opt": {
              "ipv6.opt.type": "99",
              "ipv6.opt.type_tree": {
                "ipv6.opt.type.action": "1",
                "ipv6.opt.type.change": "1",
                "ipv6.opt.type.rest": "0x00000003"
              },
              "ipv6.opt.length": "4",
              "ipv6.opt.rpl.flag": "0x00000000",
              "ipv6.opt.rpl.flag_tree": {
                "ipv6.opt.rpl.flag.o": "0",
                "ipv6.opt.rpl.flag.r": "0",
                "ipv6.opt.rpl.flag.f": "0",
                "ipv6.opt.rpl.flag.rsv": "0x00000000"
              },
              "ipv6.opt.rpl.instance_id": "0x0000001e",
              "ipv6.opt.rpl.sender_rank": "0x00000200"
            }
          }
        },
        "udp": {
          "udp.srcport": "30002",
          "udp.dstport": "3000",
          "udp.port": "30002",
          "udp.port": "3000",
          "udp.length": "31",
          "udp.checksum": "0x00007ca5",
          "udp.checksum.status": "2",
          "udp.stream": "17"
        },
        "data": {
          "data.data": "2f:14:02:15:20:ed:1a:05:02:40:29:5c:ab:41:cc:23:c7:42:10:d8:eb:41:45",
          "data.len": "23"
        }
      }
    }
  }

,

How could I remove it please? I would be very grateful if you help me please?

user6652926
  • 49
  • 2
  • 9
  • Check out this answer: https://stackoverflow.com/a/33812252/2140058 – ksbg Oct 18 '17 at 07:31
  • @treegarden it is not the last line of my file, the example that I give is repeated all the time with some modification so I need to delete the last square of each block – user6652926 Oct 18 '17 at 07:34

1 Answers1

2

First thing first: having one extra closing brace means this is not valid json, so the best thing to do would be to cure the problem at the source. If this comes verbatim from some api then contact the tech staff, if this comes from your own code then fix it where this extra brace is introduced.

This being said, assuming your json is stored as a string data, then removing the last closing brace is as simple as

data = data.strip().rstrip("}")

If this is part of an automated process and you only sometimes have this extraneaous brace, you can test before cleaning up:

if data.count("}") > data.count("{"):
    data = data.strip().rstrip("}")
bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118
  • it gives me this error: if data.count("}") > data.count("{"): AttributeError: 'file' object has no attribute 'count' – user6652926 Oct 18 '17 at 07:42
  • Please re-read my post: " assuming your json is stored __as a string__ `data`". You cannot apply string operations to files, so you obviously need to first read the file contents into a string, then process this string. – bruno desthuilliers Oct 18 '17 at 07:53