简体   繁体   中英

Python Script to convert multiple json files in to single csv

{
  "type": "Data",
  "version": "1.0",
  "box": {
    "identifier": "abcdef",
    "serial": "12345678"
  },
  "payload": {
    "Type": "EL",
    "Version": "1",
    "Result": "Successful",
    "Reference": null,
    "Box": {
      "Identifier": "abcdef",
      "Serial": "12345678"
    },
    "Configuration": {
      "EL": "1"
    },
    "vent": [
      {
        "ventType": "Arm",
        "Timestamp": "2020-03-18T12:17:04+10:00",
        "Parameters": [
          {
            "Name": "Arm",
            "Value": "LT"
          },
          {
            "Name": "Status",
            "Value": "LD"
          }
        ]
      },
      {
        "ventType": "Arm",
        "Timestamp": "2020-03-18T12:17:24+10:00",
        "Parameters": [
          {
            "Name": "Arm",
            "Value": "LT"
          },
          {
            "Name": "Status",
            "Value": "LD"
          }
        ]
      },
      {
        "EventType": "TimeUpdateCompleted",
        "Timestamp": "2020-03-18T02:23:21.2979668Z",
        "Parameters": [
          {
            "Name": "ActualAdjustment",
            "Value": "PT0S"
          },
          {
            "Name": "CorrectionOffset",
            "Value": "PT0S"
          },
          {
            "Name": "Latency",
            "Value": "PT0.2423996S"
          }
        ]
      }
    ]
  }
}

If you're looking to transfer information from a JSON file to a CSV, then you can use the following code to read in a JSON file into a dictionary in Python:

import json

with open('data.txt') as json_file:
    data_dict = json.load(json_file)

You could then convert this dictionary into a list with either data_dict.items() or data_dict.values() .

Then you just need to write this list to a CSV file which you can easily do by just looping through the list.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM