简体   繁体   中英

Loading JSON format file data to a table using python

I have JSON file in below format. I am planning to create a Python script to load the below file to a database table.

XYZABC  2017-12-04 14:51:39.712 +00:00  {"SourceRelease":"2017_R1","TargetRelease":"2017_R1.1","BlockId":6879246,"TransformType":"BeginBlock"}
XYZABC  2017-12-04 14:51:51.305 +00:00  {"OriginCode":7856,"ReplacementCode":7854,"BlockId":6879246,"TransformType":"Textitem"}
XYZABC  2017-12-04 14:51:51.339 +00:00  {"OriginCode":1497,"ReplacementCode":51143,"BlockId":6879246,"TransformType":"Textitem"}
XYZABC  2017-12-04 14:51:54.608 +00:00  {"OriginCode":7857,"ReplacementCode":7854,"BlockId":6879246,"TransformType":"Textitem"}
XYZABC  2017-12-04 14:52:05.958 +00:00  {"OriginCode":13905,"ReplacementCode":921104,"BlockId":6879246,"TransformType":"Textitem"}
XYZABC  2017-12-04 14:52:06.193 +00:00  {"OriginCode":67021,"ReplacementCode":1532551,"BlockId":6879246,"TransformType":"Textitem"}
XYZABC  2017-12-04 14:52:06.303 +00:00  {"OriginCode":4047,"ReplacementCode":1629834,"BlockId":6879246,"TransformType":"Textitem"}
XYZABC  2017-12-04 14:52:34.640 +00:00  {"Status":"Ok","BlockId":6879246,"TransformType":"EndBlock"}

Sample table column: Row

Node            : 'XYZABC'  
DateTime        : '2017-12-04 14:52:06.193'
SourceRelease   : '2017_R1'
TargetRelease   : '2017_R2'
OriginCode      : '4047'
ReplacementCode : '1629834'
BlockId         : '6879246'
TransformType   : 'Textitem'

I want every row to be loaded into those columns. Since I am very new to Python if someone can provide basic syntax in processing that JSON file format, it would be a great help.

The issue seems to be that this file does not contain pure JSON content.

So you need to split each line of text in two parts: first part should read Node and the date parts (probably with a regex ), and the second should be parsed with json.loads() .

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