簡體   English   中英

將JSOn多個對象解析為CSV

[英]Parse JSOn multiple objects to CSV

我需要幫助將具有多個對象的JSON解析為CSV,任何幫助將不勝感激。 我試圖編寫代碼,並且只能解析JSON對象,但不能解析所有對象。

我對解析內部JSON對象“ categoryName”:“ Databases”感興趣

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
  } ]
 }, {...

嘗試

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=',')

我假設您要提取數據庫列表。 訣竅在於您必須瀏覽json直到獲得有趣的列表。 然后使用csv.DictWritercsv.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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM