kata198 / jsontocsv Goto Github PK
View Code? Open in Web Editor NEWExtract data from json and convert to csv
License: GNU Lesser General Public License v3.0
Extract data from json and convert to csv
License: GNU Lesser General Public License v3.0
In a complex example one list are ignored
Json to parse (example.json) :
{
"example":[
{
"message":{
"data":"DATA_1",
"pages":{
"pages":[
{
"lines":{
"lines":[
{
"content":"CONTENT_1_1"
},
{
"content":"CONTENT_1_2"
},
{
"content":"CONTENT_1_3"
}
]
},
"duration":"DURATION_1"
}
]
},
"pictures":"PICTURES_1"
}
},
{
"message":{
"data":"DATA_2",
"pages":{
"pages":[
{
"lines":{
"lines":[
{
"content":"CONTENT_2_1"
},
{
"content":"CONTENT_2_2"
},
{
"content":"CONTENT_2_3"
}
]
},
"duration":"DURATION_2"
}
]
},
"pictures":"PICTURES_2"
}
}
]
}
Parse string :
PARSE_STR = '''
+"example"[
."message"[
"data",
"pictures"
."pages"[
+"pages"[
"duration"
."lines"[
+"lines"[
"content"
]
]
]
]
]
]
'''
Python Code
with open('example.json', 'rt') as f:
contents = f.read()
parser = JsonToCsv(PARSE_STR, nullValue=NULL_VALUE, debug=IS_DEBUG)
csvDataStr = parser.convertToCsv(contents)
output (second content list are ignored) :
DATA_1,PICTURES_1,DURATION_1,CONTENT_1_1
DATA_1,PICTURES_1,DURATION_1,CONTENT_1_2
DATA_1,PICTURES_1,DURATION_1,CONTENT_1_3
DATA_2,PICTURES_2,DURATION_2
expected output :
DATA_1,PICTURES_1,DURATION_1,CONTENT_1_1
DATA_1,PICTURES_1,DURATION_1,CONTENT_1_2
DATA_1,PICTURES_1,DURATION_1,CONTENT_1_3
DATA_2,PICTURES_2,DURATION_2,CONTENT_2_1
DATA_2,PICTURES_2,DURATION_2,CONTENT_2_2
DATA_2,PICTURES_2,DURATION_2,CONTENT_2_3
I am trying to parse a json that consists in a list of maps such as [{"a":"1}, {"a":3}]
but I am not being able to define the parse string for it. A simple +
demands a key, which means that this list would need to be contained in a map. Am I missing something?
Thanks
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.