将JSOn多个对象解析为CSV

时间:2018-08-08 22:52:04

标签: python json csv

我需要帮助将具有多个对象的JSON解析为CSV,任何帮助将不胜感激。我试图编写代码,但只能解析JSON对象,但不能解析所有对象。

我有兴趣解析内部JSON对象“ categoryName”:“数据库”

import requests
import json
import csv


from requests.auth import HTTPBasicAuth
r = requests.get('https://url')
properties = r.json()['categories']


# open a file for writing

csv_data = open('/home/sourcecode/csv_data.csv', 'w')

# create the csv writer object

csvwriter = csv.writer(csv_data)

count = 0

for temp in properties:

  if count == 0:

         header = temp.keys()

         csvwriter.writerow(header)

         count += 1

  csvwriter.writerow(temp.values())

csv_data.close()

我不会发布完整的JSON,因为它很冗长,只是发布一部分,以便在下面更好地理解。

    {
"categories" : [ {
  "categoryName" : "HDFSEncryptionZones",
"metrics" : [ {
  "metricName" : "EncryptionZone Object Count",
  "value" : 0
}, {
  "metricName" : "Out of EncryptionZone Objects Count",
  "value" : 0
} ]
}, {
  "categoryName" : "Databases",
  "metrics" : [ {
  "metricName" : "No. of Databases",
  "value" : 78
 }, {
  "metricName" : "Top 5 Databases (by no of tables)",
  "value" : [ {
    "name" : "abc",
    "value" : 1234,
    "id" : 1187422
  }, {
    "name" : "def",
    "value" : 578,
    "id" : 8194003
  }, {
    "name" : "ghi",
    "value" : 241,
    "id" : 1214282
  }, {
    "name" : "jkl",
    "value" : 214,
    "id" : 11677477
  }, {
    "name" : "mno",
    "value" : 186,
    "id" : 6716158
  }, {
    "name" : "pqr",
    "value" : 130,
    "id" : 59489134
  }, {
    "name" : "stu",
    "value" : 102,
    "id" : 59489133
  }, {
    "name" : "xyz",
    "value" : 96,
    "id" : 11630638
  }, {
    "name" : "temp",
    "value" : 80,
    "id" : 100074536
  }, {
    "name" : "test",
    "value" : 72,
    "id" : 59489132
  } ]
  } ]
 }, {
  "categoryName" : "Storage",
  "metrics" : [ {
   "metricName" : "No. of S3 Objects.",
   "value" : 0
 }, {
   "metricName" : "No. of HDFS Objects.",
   "value" : 3097309
  } ]
 }, {...

2 个答案:

答案 0 :(得分:0)

尝试

for category in properties:
  if category['categoryName'] == "Databases":
      for metric in category['metrics']:
        if metric['metricName'] == "Top 5 Databases (by no of tables)":
          for m in metric['value']:
            print(m['name'],m['value'],m['id'],sep=',')

答案 1 :(得分:0)

我假设您要提取数据库列表。诀窍在于您必须浏览json直到获得有趣的列表。那么csv.DictWriter是必经之路,因为您将有一个字典列表。代码可能是:

import requests
import json
import csv


from requests.auth import HTTPBasicAuth
r = requests.get('https://url')
properties = r.json()['categories']


for temp in properties:
    if "Databases" == temp['categoryName']:     # first locate "categoryName": " Databases"
        for kind in temp['metrics']:
            for value in kind.values():
                if isinstance(value, list):     # next find a list value
                    # open a file for writing (with ensure close on end of block)
                    with open('/home/sourcecode/csv_data.csv', 'w', newline = '') as csv_data:
                        # create the csv writer object
                        writer = csv.DictWriter(fd, fieldnames = value[0].keys())
                        writer.writeheader()       # first the header
                        for base in value:         # then the values
                            writer.writerow(base)