Convert json file to csv
NickName:FerFabbiano Ask DateTime:2020-08-06T08:15:51

Convert json file to csv

I have this simple python code that converts a json file into a csv. I would like to convert only the first four values of each key, but i couldn't figure out how to do it.

import json 
import csv 
  
  
# Opening JSON file and loading the data 
# into the variable data 
with open('personas.json') as json_file: 
    data = json.load(json_file) 
  
employee_data = data['emp_details'] 
  
# now we will open a file for writing 
data_file = open('data_file.csv', 'w') 
  
# create the csv writer object 
csv_writer = csv.writer(data_file) 
  
# Counter variable used for writing  
# headers to the CSV file 
count = 0
  
for emp in employee_data: 
    if count == 0: 
  
        # Writing headers of CSV file 
        header = emp.keys() 
        csv_writer.writerow(header) 
        count += 1
  
    # Writing data of CSV file 
    csv_writer.writerow(emp.values()) 
  
data_file.close() 

Here is an example of json file's format

{"emp_details":[
    {
     "DATAID":"6908443",
     "FIRST_NAME":"Fernando",
     "SECOND_NAME":"Fabbiano",
     "THIRD_NAME":"Agustin",
     "FOURTH_NAME":"", 
     "AGE": "21", 
     "EMAIL": "[email protected]"
    }
]}

And as i said, i would like to convert only the fields DATAID, FIRSTNAME, SECONDNAME, THIRD NAME.

Copyright Notice:Content Author:「FerFabbiano」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/63275168/convert-json-file-to-csv

More about “Convert json file to csv” related questions

How to convert .csv file to json?

I need to read a .csv file and convert the csv file to json format and pass the json to frontend which is the best way to read a csv file and convert to json Here my code is: fs.readFile(req.files.

Show Detail

Convert json file to csv

I have this simple python code that converts a json file into a csv. I would like to convert only the first four values of each key, but i couldn't figure out how to do it. import json import csv ...

Show Detail

Convert CSV to JSON file in python

Above csv file which contains nearly 2000 rows. I want to parse CSV file line by line and convert it to JSON and send over websocket. I found some code online which converts CSV to JSON as follo...

Show Detail

Convert csv to json returning empty json file

I am trying to convert a csv file to json using the convert-csv-to-json package from npm. I am able to get the csv from the url and the 'data.csv' is created, but the corresponding json file is jus...

Show Detail

How to convert blueprint json file to csv file?

How to convert blueprint json file to csv file? My target is to convert all properties parameters to a csv file from the amabri cluster Example – how to generate new fresh blueprint.json file fro...

Show Detail

How to convert JSON to CSV and then save to computer as CSV file

I'm currently trying to pull json data from and API, convert it to csv using the json2csv node.js module, and then save the data as a csv file on my laptop. However, when I run the script, nothing

Show Detail

convert csv (;) deliminated file to Json file

I try to convert CSV file(; delimited) to JSON file and my CSV file structure is like below sample: ID;NAME;AGE;CLASS;SEC 1;ram;13;8;B like that import csv,json csvf_path='xyz' jsonf_path='mnb...

Show Detail

Convert JSON to CSV in nifi

I want to convert JSON files to CSV in nifi. We can achieve this in Python and other programming languages and have multiple articles on it. I have multiple JSON files and each file has different s...

Show Detail

how to convert CSV file to json in flutter?

recently i develop an ToDo application using flutter and sqflite , for backup my data i convert my database data to csv file(import data to csv file using csv package) now i want to again use this ...

Show Detail

convert a CSV file to JSON file

I am trying to convert CSV file to JSON file based on a column value. The csv file looks somewhat like this. ID Name Age CSE001 John 18 CSE002 Marie ...

Show Detail