Last active
May 29, 2019 18:20
-
-
Save venkatsgithub1/ad6860f70c2e47082f01b149c91ff8cd to your computer and use it in GitHub Desktop.
Loading a json file and writing to csv in python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import csv | |
with open('./test.json', 'r') as f1: | |
# read data as string. | |
data_json = f1.read() | |
# loads method converts string data into dictionary. | |
dict1=json.loads(data_json) | |
print(dict1) | |
# accessing data | |
# access td first, from td, access a. | |
print(dict1['td']['a']) | |
# access arr first, from arr access 0th index, which has a2 data, once at 0th index, access a2 data. | |
print(dict1['arr'][0]['a2']) | |
# writing to csv | |
with open('./output.csv', 'w') as f2: | |
csv_writer = csv.writer(f2) | |
csv_writer.writerow(['A','B']) | |
csv_writer.writerow([dict1['td']['a'], dict1['arr'][0]['a2']]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import csv | |
with open('./test_2.json', 'r') as f1: | |
# read data as string. | |
data_json = f1.read() | |
# loads method converts string data into dictionary. | |
list1=json.loads(data_json) | |
print(list1) | |
# accessing data | |
for row in list1: | |
print(row) | |
# access row by index | |
print(list1[0]) | |
# # writing to csv | |
with open('./output2.csv', 'w') as f2: | |
csv_writer = csv.writer(f2) | |
csv_writer.writerow(['A','B']) | |
for row in list1: | |
csv_writer.writerow([row['td']['a'], row['arr'][0]['a2']]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
A | B | |
---|---|---|
Apple | Azerbaijan |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
A | B | |
---|---|---|
Apple | Azerbaijan | |
Apricot | Argentina |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"td":{ | |
"a":"Apple", | |
"b":"Bat" | |
}, | |
"cd":{ | |
"a1":"Al", | |
"b1":"Bob" | |
}, | |
"arr":[ | |
{ | |
"a2":"Azerbaijan" | |
}, | |
{ | |
"b2":"Baku" | |
} | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
{ | |
"td":{ | |
"a":"Apple", | |
"b":"Bat" | |
}, | |
"cd":{ | |
"a1":"Al", | |
"b1":"Bob" | |
}, | |
"arr":[ | |
{ | |
"a2":"Azerbaijan" | |
}, | |
{ | |
"b2":"Baku" | |
} | |
] | |
}, | |
{ | |
"td":{ | |
"a":"Apricot", | |
"b":"Bus" | |
}, | |
"cd":{ | |
"a1":"Aaron", | |
"b1":"Brian" | |
}, | |
"arr":[ | |
{ | |
"a2":"Argentina" | |
}, | |
{ | |
"b2":"Buenos Aires" | |
} | |
] | |
} | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment