-1

This is my JSON response and i want to drop the whole meta header part.

{
    "meta": {
        "limit": 1000,
        "next": "https://cisco-demo.obsrvbl.com/api/v3/observations/all/?limit=1000&offset=1000",
        "offset": 0,
        "previous": null,
        "total_count": 1863020
    },
    "objects": [
        {
            "creation_time": null,
            "end_time": "2017-08-17T19:40:00Z",
            "id": 281,
            "observation_name": "External Port Scanner",
            "port_count": 251,
            "port_ranges": "0-1023",
            "resource_name": "port_scanner_external_v1",
            "scan_type": "inbound",
            "scanned_ip": "209.182.184.2",
            "scanned_ip_country_code": "US",
            "scanned_packets": 999,
            "scanner_ip": "130.126.24.53",
            "scanner_ip_country_code": "US",
            "scanner_packets": 997,
            "source": 109,
            "time": "2017-08-17T19:40:00Z"
        },
        {
            "creation_time": null,
            "end_time": "2017-08-17T19:50:00Z",
            "id": 304,
            "observation_name": "External Port Scanner",
            "port_count": 41,
            "port_ranges": "0-1023",
            "resource_name": "port_scanner_external_v1",
            "scan_type": "inbound",
            "scanned_ip": "209.182.184.2",
            "scanned_ip_country_code": "US",
            "scanned_packets": 152,
            "scanner_ip": "130.126.24.53",
            "scanner_ip_country_code": "US",
            "scanner_packets": 152,
            "source": 109,
            "time": "2017-08-17T19:50:00Z"
        },
DirtyBit
  • 16,613
  • 4
  • 34
  • 55
  • 1
    welcome to stackoverflow. what have you tried? what errors are you getting? did you try the python [JSON](https://docs.python.org/3.6/library/json.html) module? a direction for you to follow would be: convert your json to a python object, remove the part you dont need and convert it back to a json – Nullman Mar 18 '19 at 09:03

1 Answers1

0
import json

with open('myfile.txt') as fp:
    my_dict = json.loads(fp.read())

try:
    del my_dict['meta']
except KeyError:
    pass

I'm assuming you already have your JSON as dictionary, in the code above I just load it from a text file. The command you're looking for is del. It's a good idea to have it in a try/catch block just in case you're dealing with a dictionary that doesn't have the meta field, because in that case it would throw a KeyError and break.

TomMP
  • 715
  • 1
  • 5
  • 15