I have a JSON File that looks like this:
(我有一个看起来像这样的JSON文件:)
{
"tenantId" : "005237f9-6779-4c68-89d6-d0800559d4f4",
"id" : "005237f9-6779-4c68-89d6-d0800559d4f4/10000072",
"erpId" : "10000072",
"branch" : null,
"importExport" : null,
"globalShipmentId" : "GALR0000098005",
"status" : "Complete",
"creationDateErp" : "2018-02-27T11:32:09",
"creationDateTime" : "2019-10-09T21:23:17.189",
"executionDateTime" : null,
"type" : null,
"mode" : "SEA",
"pickupAddress" : null,
"deliveryAddress" : null,
"portOfLoading" : {
"code" : "VCI",
"codeIATA" : "VCI",
"name" : "VALENCIA",
"city" : null,
"state" : null,
"country" : "ES",
"etd" : "2018-02-27T00:00",
"eta" : null
},
"portOfDischarge" : {
"code" : "VCI",
"codeIATA" : "VCI",
"name" : "VALENCIA",
"city" : null,
"state" : null,
"country" : "ES",
"etd" : null,
"eta" : "2018-02-27T00:00"
},
"incoTerms" : "DDP",
"totalPieces" : 807,
"chargeableWeightConverted" : 0.0,
"chargeableWeight" : null,
"chargeableWeightUnits" : null,
"calculatedWeightUnits" : null,
"calculatedVolumeConverted" : 0.0,
"calculatedVolume" : 0.0,
"calculatedVolumeUnits" : null,
"totalWeightConverted" : 0.0,
"totalWeight" : 10693.0,
"totalWeightUnits" : "Kilogram",
"totalVolumeConverted" : 0.0,
"totalVolume" : null,
"totalVolumeUnits" : "CubicMeter",
"shipper" : null,
"consignee" : null,
"notify" : null,
"isAllIn" : false,
"mainFreight" : {
"freight" : {
"carrierId" : null,
"carrierErpId" : null,
"carrierName" : null,
"carrierCode" : null,
"motherVesselName" : null,
"motherVoyage" : null,
"motherVesselDepartDateTime" : null,
"motherVesselArvDateTime" : null,
"loadTerm" : "FCL",
"motherVesselCallSign" : null,
"motherVesselCode" : null,
"motherVesselFlag" : null,
"feederVesselName" : null,
"feederVesselVoyage" : null,
"feederVesselDepartDate" : null,
"feederVesselDepartTime" : null,
"feederVesselArvDate" : null,
"feederVesselArvTime" : null,
"feederVesselCallSign" : null,
"feederVesselCode" : null,
"totalWeightToVolumeRatio" : null,
"convertedFreightChargePerUnit" : null,
"freightChargePerUnit" : null,
"freightChargeCurrency" : "USD",
"freightChargeUnit" : "22G1",
"numberOfUnits" : null,
"convertedFreightTotCost" : null,
"freightTotCost" : null,
"totalCostCurrency" : null,
"convertedFreightTotRevenue" : null,
"freightTotRevenue" : null,
"totalRevenueCurrency" : null,
"totalNoOfPieces" : null,
"breakBulkPartyId" : null,
"breakBulkAgentName" : null,
"mode" : "SEA"
},
"freightPieces" : [ {
"freightPieceId" : 0,
"freightPieceType" : "CONTAINER",
"subType" : "22G1",
"heightConverted" : null,
"height" : null,
"widthConverted" : null,
"width" : null,
"lengthConverted" : null,
"length" : null,
"measurementUnits" : null,
"pieceWeightConverted" : null,
"pieceWeight" : null,
"pieceWeightUnits" : null,
"pieceVolumeConverted" : null,
"pieceVolume" : null,
"pieceVolumeUnits" : null,
"hazardous" : null,
"hazardClass" : null,
"commodityType" : null,
"commodityDescription" : null,
"quantity" : 1
} ]
},
"originDetails" : null,
"destinationDetails" : null,
"houseDocumentNumber" : null,
"houseDocumentDate" : null,
"masterDocumentNumber" : "461130393747",
"masterDocumentDate" : null,
"invoices" : null,
"createdBy" : null,
"createByName" : null
}
I'm not sure how to parse it correctly.
(我不确定如何正确解析它。)
It seems like it parses most of the lines fine into a CSV but it keeps the parenthesis in the line "Posrtofdischarge"
and it parses it fine again in the line "incoterms"
. (似乎它将大多数行都解析为CSV,但将括号保留在"Posrtofdischarge"
并再次将其解析为"incoterms"
。)
The problem keeps returning in the JSON every time there is a new "{}". (每当有新的“ {}”时,问题就不断在JSON中返回。)
What can be the reason? (可能是什么原因?)
The code which i used is:
(我使用的代码是:)
import json
import os
from glob import glob
import csv
def JSONParse():
'''
curdir = input("Please enter your json dir")
os.chdir(curdir)
'''
os.chdir('C:OutputJSON')
global data
data = []
global collist
collist = []
for file_name in glob('*.json'):
with open(file_name) as f:
data = json.loads("[" + f.read().replace("}
{", "},
{") + "]") #json creation for each file
def ColParse():
for da in data:
for key in da.keys():
if key not in collist:
collist.append(key.split(":")[0])
collist.append("CustomerID") # appending file name and ip from so it can be added to the csv.
collist.append("Timestamp")
print(collist)
def CSVWrite():
count = 0
with open("postgres.csv", 'a') as c:
thewriter = csv.DictWriter(c, fieldnames=collist)
if count == 0:
thewriter.writeheader()
thewriter.writerows(data)
count = 1
def main():
JSONParse()
ColParse()
CSVWrite()
if __name__ == "__main__":
main()
ask by Yuval Ezrati translate from so