liv2hak
liv2hak

Reputation: 14970

Python program to extract data from JSON files

I have a python file with both data and meta data.I use the following program on that file.

import requests
from lxml import html
import json

with open("tmp.json") as json_file:
        json_data = json.load(json_file)
        print(json_data)

In the output I get both data and metadata in the JSON file. I want to extract only the data part.How can I do that?

Sample structure of my JSON file is as follows.

 "data": [
        {
            "AvInterval": null,
            "DelInterval": null,
            "ntsGenerated": null,
            "ntsGenerated": null,
            "Metric": "Intervals",
            "Total": 23
        },
        {
            "AvInterval": null,
            "DelInterval": null,
            "ntsGenerated": null,
            "ntsGenerated": null,
            "Metric": "CPU",
            "Total": 47
        },
      ],
"metadata": {
       "columns": [
        {
            "Caption": "Metric",
            "Field": "Metric",
            "Type": "string",
            "Width": "*"
        },
        {
            "Caption": "Total",
            "Field": "Total",
            "Type": "long",
            "Width": "*"
        },
  ]

}

I want to remove the metadata from the above files and cut the data into different pieces and write each separate piece into a file.

Upvotes: 1

Views: 1798

Answers (3)

Shanki
Shanki

Reputation: 167

Assuming the json below, try this

import simplejson as json

with open("tmp.json", "r") as json_file:
    extracted_data = json.loads(json_file)

    for data in  extracted_data['data']:
        print data['AvInterval'], data['Metric'], data['DelInterval']

The json sample below

{
    "data": [
            {
                "AvInterval": null,
                "DelInterval": null,
                "ntsGenerated": null,
                "ntsGenerated": null,
                "Metric": "Intervals",
                "Total": 23
            },
            {
                "AvInterval": null,
                "DelInterval": null,
                "ntsGenerated": null,
                "ntsGenerated": null,
                "Metric": "CPU",
                "Total": 47
            }
          ],
    "metadata": {
           "columns": [
            {
                "Caption": "Metric",
                "Field": "Metric",
                "Type": "string",
                "Width": "*"
            },
            {
                "Caption": "Total",
                "Field": "Total",
                "Type": "long",
                "Width": "*"
            }
      ]
    }
}

Upvotes: 0

Lee
Lee

Reputation: 2730

If you want to cut the data into different pieces and write each piece in to its own file, you could do something like this:

with open("tmp.json") as json_file:
    json_data = json.load(json_file)
    for i, data_item in enumerate(json_data['data']):
        fname = 'data_%s' % i
        with open(fname, 'w') as outfile:
            json.dump(data_item, outfile)

Upvotes: 2

sumit-sampang-rai
sumit-sampang-rai

Reputation: 691

Use the data key

json_data['data']

Upvotes: 1

Related Questions