Converting CSV to jSON - Keep getting "End of File expected" error - python

I'm trying to convert a CSV file into a jSON file in order to then inject it into a Firebase database.
csvfile = open('final_df_2.csv', 'r')
jsonfile = open('final_df_5.json', 'w')
reader = csv.DictReader(csvfile)
for row in reader:
json.dump({row['ballID']: [{"colour": row['colour'], "radius":row['radius']}]}, jsonfile)
jsonfile.write('\n')
Unfortunately, I keep getting an "End of File expected" error
Here's my JSON's output
{
"001": [
{
"colour": "green",
"radius": "199405.0"
}
]
}
{
"002": [
{
"colour": "blue",
"radius": "199612.0"
}
]
}
In addition, Firebase sends back an error when I try to import the JSON file saying "Invalid JSON files"

You could collect all the data into a python list and dump that list to the json file:
csvfile = open('final_df_2.csv', 'r')
reader = csv.DictReader(csvfile)
jsoncontent = []
for row in reader:
jsoncontent.append({row['ballID']: [{"colour": row['colour'], "radius":row['radius']}]})
with open('final_df_5.json', 'w') as jsonfile:
json.dump(jsoncontent, jsonfile)
However, I'm not sure what your firebase database is expecting.

Related

Converting CSV file to .json file in a specific format using python

I have the below input.csv file and I'm having trouble in converting it to a .json file.
Below is the input.csv file that I have which I want to convert it into .json file. The Text field is in Sinhala Language
Date,Text,Category
2021-07-28,"['ලංකාව', 'ලංකාව']",Sports
2021-07-28,"['ඊයේ', 'ඊයේ']",Sports
2021-07-29,"['ලංකාව', 'ලංකාව', 'ලංකාව', 'ලංකාව']",Sports
2021-07-29,"['ඊයේ', 'ඊයේ', 'ඊයේ', 'ඊයේ']",Sports
2021-08-01,"['ලංකාව', 'ලංකාව', 'ලංකාව', 'ලංකාව']",Sports
The .json format that I want to have is as of below
[
{
"category":"Sports",
"date":"2021-07-28",
"data": ['ලංකාව', 'ලංකාව']
},
{
"category":"Sports",
"date":"2021-07-28",
"data": ['ඊයේ', 'ඊයේ']
},
{
"category":"Sports",
"date":"2021-07-29",
"data": ['ලංකාව', 'ලංකාව', 'ලංකාව', 'ලංකාව']
},
{
"category":"Sports",
"date":"2021-07-29",
"data": ['ඊයේ', 'ඊයේ', 'ඊයේ', 'ඊයේ']
},
{
"category":"Sports",
"date":"2021-08-01",
"data": ['ලංකාව', 'ලංකාව', 'ලංකාව', 'ලංකාව']
}
]
Below is how I tried, since this is of Sinhala Language, values are show in this format \u0d8a\u0dba\u0dda, which is another thing that I'm struggling to sort out. And the json format is also wrong that I expect it to be.
import csv
import json
def toJson():
csvfile = open('outputS.csv', 'r', encoding='utf-8')
jsonfile = open('file.json', 'w')
fieldnames = ("date", "text", "category")
reader = csv.DictReader(csvfile, fieldnames)
out = json.dumps([row for row in reader])
jsonfile.write(out)
if __name__ == '__main__':
toJson()
Use ensure_ascii=False when doing json.dumps:
out = json.dumps([row for row in reader], ensure_ascii=False)
Other notes:
Since the first row of the csv contains the column names, you should either skip this first row, or let csv.DictReader use the first row as the column names automatically by not passing explicit values to fieldnames.
It's very bad practice to use open and then not close it.
To make things easier you can use a with statement.
The second column of the csv file will be treated as a string and not as a list of strings unless you specifically parse it as such (you can use literal_eval from the ast module for this).
You can use json.dump instead of json.dumps to write directly to the file.
With this, you can rewrite your function to:
def toJson():
with (open('delete.csv', 'r', encoding='utf-8') as csvfile,
open('file.json', 'w') as jsonfile):
fieldnames = ("date", "text", "category")
reader = csv.DictReader(csvfile, fieldnames)
next(reader) # skip header row
json.dump([row for row in reader], jsonfile, ensure_ascii=False)
Read your CSV using pandas # using pd.read_csv()
use to_dict function with orient option set to records
df = pd.read_csv('your_csv_file_name.csv')
df.to_dict(orient='records')

Convert CSV file to JSON with python

I am trying to covert my CSV email list to a JSON format to mass email via API. This is my code thus far but am having trouble with the output. Nothing is outputting on my VS code editor.
import csv
import json
def make_json(csvFilePath, jsonFilePath):
data = {}
with open(csvFilePath, encoding='utf-8') as csvf:
csvReader = csv.DictReader(csvf)
for rows in csvReader:
key = rows['No']
data[key] = rows
with open(jsonFilePath, 'w', encoding='utf-8') as jsonf:
jsonf.write(json.dumps(data, indent=4))
csvFilePath = r'/data/csv-leads.csv'
jsonFilePath = r'Names.json'
make_json(csvFilePath, jsonFilePath)
Here is my desired JSON format
{
"EmailAddress": "hello#youngstowncoffeeseattle.com",
"Name": "Youngstown Coffee",
"ConsentToTrack": "Yes"
},
Heres my CSV list
No,EmailAddress,ConsentToTrack
Zylberschtein's Delicatessen & Bakery,catering#zylberschtein.com,Yes
Youngstown Coffee,hello#youngstowncoffeeseattle.com,Yes
It looks like you could use a csv.DictReader to make this easier.
If I have data.csv that looks like this:
Name,EmailAddress,ConsentToTrack
Zylberschtein's Delicatessen,catering#zylberschtein.com,yes
Youngstown Coffee,hello#youngstowncoffeeseattle.com,yes
I can convert it into JSON like this:
>>> import csv
>>> import json
>>> fd = open('data.csv')
>>> reader = csv.DictReader(fd)
>>> print(json.dumps(list(reader), indent=2))
[
{
"Name": "Zylberschtein's Delicatessen",
"EmailAddress": "catering#zylberschtein.com",
"ConsentToTrack": "yes"
},
{
"Name": "Youngstown Coffee",
"EmailAddress": "hello#youngstowncoffeeseattle.com",
"ConsentToTrack": "yes"
}
]
Here I've assumed the headers in the CSV can be used verbatim. I'll update this with an exmaple if you need to modify key names (e.g. convert "No" to "Name"),.
If you need to rename a column, it might look more like this:
import csv
import json
with open('data.csv') as fd:
reader = csv.DictReader(fd)
data = []
for row in reader:
row['Name'] = row.pop('No')
data.append(row)
print(json.dumps(data, indent=2))
Given this input:
No,EmailAddress,ConsentToTrack
Zylberschtein's Delicatessen,catering#zylberschtein.com,yes
Youngstown Coffee,hello#youngstowncoffeeseattle.com,yes
This will output:
[
{
"EmailAddress": "catering#zylberschtein.com",
"ConsentToTrack": "yes",
"Name": "Zylberschtein's Delicatessen"
},
{
"EmailAddress": "hello#youngstowncoffeeseattle.com",
"ConsentToTrack": "yes",
"Name": "Youngstown Coffee"
}
]
and to print on my editor is it simply print(json.dumps(list(reader), indent=2))?
I'm not really familiar with your editor; print is how you generate console output in Python.

find and replace specific json data in python?

I am trying to replace data1 with data5 in json body of Full Address, but my code is giving error with these (list indices must be integers or slices, not str).
Please see my code as well as json body below.
json
[
{
"fields": {
"Full Address": "data1",
"pl/p/0": "212152",
"ot": "fgde"
}
},
{
"fields": {
"Full Address": "data2",
"pl/p/0": "52562",
"ot": "frtfr"
}
}
]
code
import json
with open('jsonbody.json') as f:
data = json.load(f)
for item in data['fields']:
item['Full Address'] = item['Full Address'].replace('data1', 'data5')
with open('new_data.json', 'w') as f:
json.dump(data, f)
the data is list, and its element is a dict, which contains fields key
import json
with open('jsonbody.json') as f:
data = json.load(f)
for item in data:
item['fields']['Full Address'] = item['fields']['Full Address'].replace('data1', 'data5')
with open('new_data.json', 'w') as f:
json.dump(data, f)
Here I am answering my own question with adding german umlaut detail.
import json
jsonbody = 'jsonbody.encode()'
with open('jsonbody.json', 'r') as file:
json_data = json.load(file)
for item in json_data:
if item['fields']['Full Address'] in ["data1"]:
item['fields']['Full Address'] = "data5_Ä"
with open('new_data.json', 'w') as file:
json.dump(json_data, file, indent=2)
# if my detail contain german words then I will use this
json.dump(json_data, file, indent=2, ensure_ascii=False)

Trouble with this error while adding a dict to an existing key value

Code Including JSON File Code:
Python( Suppose To Append A New User and Balance):
import json
with open('users_balance.json', 'r') as file:
data = json.load(file)['user_list']
data['user_list']
data.append({"user": "sdfsd", "balance": 40323420})
with open('users_balance.json', 'w') as file:
json.dump(data, file, indent=2)
Json(Object The Code Is Appending To):
{
"user_list": [
{
"user": "<#!672986823185661955>",
"balance": 400
},
{
"user": "<#!737747404048171043>",
"balance": 500
}
],
}
Error(Traceback Error Given After Executing Code):
data = json.load(file)['user_list']
KeyError: 'user_list'
The solution is this:
import json
with open('users_balance.json', 'r') as file:
data = json.load(file)
data['user_list'].append({"user": "sdfsd", "balance": 40323420})
with open('users_balance.json', 'w') as file:
json.dump(data, file, indent=2)

converting csv file into json format in python

I have written a code to convert my csvfile which is '|' delimited file to get specific json format.
Csv file format:
comment|address|city|country
crowded|others|others|US
pretty good|others|others|US ....
I have tried with other codes as well since I'm new to python I'm stuck in between. If somebody helps me to correct the mistake I'm doing it would be helpful.
import csv
import json
from collections import OrderedDict
csv_file = 'test.csv'
json_file = csv_file + '.json'
def main(input_file):
csv_rows = []
with open(input_file, 'r') as csvfile:
reader = csv.DictReader(csvfile)
title = reader.fieldnames
for row in reader:
entry = OrderedDict()
for field in title:
entry[field] = row[field]
csv_rows.append(entry)
with open(json_file, 'w') as f:
json.dump(csv_rows, f, sort_keys=True, indent=4, ensure_ascii=False)
f.write('\n')
if __name__ == "__main__":
main(csv_file)
I want in json format as below
{
"reviewer": {
"city": "",
"country": ""
"address": "Orlando, Florida"
},
But I'm getting output like this:
[
{
"COMMENT|\"ADDRESS\"|\"CITY\"|"COUNTRY":"Crowded"|"Others"|"Others"|
},
{
"COMMENT|\"ADDRESS\"|\"CITY\"|"COUNTRY":"pretty good"|"Others"|"Others"|
},
You're missing the separator parameter. Instead of:
reader = csv.DictReader(csvfile)
Use:
reader = csv.DictReader(csvfile, delimiter='|')

Categories