Json value separate from the convert csv to json - python

I want to convert the data to a csv file with python json, after my run went well as the results below.
[{"id": "1", "name": "billy", "job": "web-develop"}, {"id": "1", "name": "smith", "job": "programming"}]
but none the less I think, how to separate json results based "," as shown below.
[{"id": "1", "name": "billy", "job": "web-develop"},
{"id": "1", "name": "smith", "job": "programming"}]
This myscript
import csv
import json
import sys
input = str(sys.argv[1])
csvfile = open(input, 'r')
fieldnames = ("id","name","job")
reader = csv.DictReader( csvfile, fieldnames)
out = json.dumps( [ row for row in reader ] )
sys.stdout.write(out)

Related

Convert Nested JSON into CSV File

I want to create a CSV file with the columns 'Duration' and 'Attention' from a nested JSON file that looks like this:
[
{
"Id": {
"Address": "",
"Address": "",
"Name": "Avg"
},
"position": 0,
"sender": [
false
],
"Atten": 0,
"Duration": 5,
"End": 1645,
"EndRaw": 16040.395,
"Index": 0,
"__v": 0
},
{
"Id": {
"local": "",
"Address": "",
"trafficPairName": "Avg"
},
"position": 0,
"sender": [
false
],
"Atten": 1,
"Duration": 5,
"End": 1652,
"EndRaw": 166140.3618,
"Index": 1,
"__v": 0
}
]
I am getting an error on the for loop that says "list indices must be integers or slices, not str". How do I properly parse this JSON file and create the CSV file?
Here is my Python code:
import json
import csv
with open('averages.json') as f:
data = json.load(f)
filename = "data.csv"
with open(filename, "w") as file:
csv_file = csv.writer(file)
csv_file.writerow(["Duration", "Attention"])
for item in data["Id"]:
csv_file.writerow([])
data is list of dicts, and from each dict you want the values for pair of keys.
for item in data["pairId"]:
csv_file.writerow([])
should be
for item in data:
csv_file.writerow([item.get("stepDuration"), item.get("stepIndexAtten")])
or in one-line list comprehension
csv_file.writerows(([item.get("stepDuration"), item.get("stepIndexAtten")] for item in data))
Not sure why are you taking the data["pairId"] if you need the stepDuration and stepIndexAtten?
This should work for you,
import json
import csv
with open('averageThroughputVsTime-Aggregated.json') as f:
data = json.load(f)
fname = "output.csv"
with open(fname, "w") as file:
csv_file = csv.writer(file)
csv_file.writerow(["stepDuration", "stepIndexAtten"])
for key, value in enumerate(data):
csv_file.writerow([key, value["stepDuration"], value["stepIndexAtten"]])
output.csv
stepDuration,stepIndexAtten
0,5,0
1,5,1

Convert CSV with nested headers to JSON

So far, I have this code (with help from a tutorial):
import csv, json
csvFilePath = "convertcsv.csv"
jsonFilePath = "newResult.json"
# Read the CSV and add the data to a dictionary...
data = {}
with open(csvFilePath) as csvFile:
csvReader = csv.DictReader(csvFile)
for csvRow in csvReader:
data = csvRow
# Write data to a JSON file...
with open(jsonFilePath, "w") as jsonFile:
jsonFile.write(json.dumps(data, indent=4))
My desired output is this:
{
"userID": "string",
"username": "string",
"age": "string",
"location": {
"streetName": "string",
"streetNo": "string",
"city": "string"
}
}
I don't know how to represent the "location".
My actual result is this:
{
"userID": "string",
"username": "string",
"age": "string",
"location/streetName": "string",
"location/streetNo": "string",
"location/city": "string",
}
How can I seperate streetName, streetNo and city and put them into "location"?
Below is a simple script should do what you want. The result will be a json object with the "userID" as keys. Note that, to test deeper nesting, I used a csv file with slightly different headers - but it will work just as well with your original example.
import csv, json
infile = 'convertcsv.csv'
outfile = 'newResult.json'
data = {}
def process(header, value, record):
key, other = header.partition('/')[::2]
if other:
process(other, value, record.setdefault(key, {}))
else:
record[key] = value
with open(infile) as stream:
reader = csv.DictReader(stream)
for row in reader:
data[row['userID']] = record = {}
for header, value in row.items():
process(header, value, record)
with open(outfile, "w") as stream:
json.dump(data, stream, indent=4)
INPUT:
userID,username,age,location/street/name,location/street/number,location/city
0,AAA,20,This Street,5,This City
1,BBB,42,That Street,5,That City
2,CCC,34,Other Street,5,Other City
OUTPUT:
{
"0": {
"userID": "0",
"username": "AAA",
"age": "20",
"location": {
"street": {
"name": "This Street",
"number": "5"
},
"city": "This City"
}
},
"1": {
"userID": "1",
"username": "BBB",
"age": "42",
"location": {
"street": {
"name": "That Street",
"number": "5"
},
"city": "That City"
}
},
"2": {
"userID": "2",
"username": "CCC",
"age": "34",
"location": {
"street": {
"name": "Other Street",
"number": "5"
},
"city": "Other City"
}
}
}
I'd add some custom logic to achieve this, note that this is for the first level only, if you want more, you should create a recoursive function:
# Write data to a JSON file...
with open(jsonFilePath, "w") as jsonFile:
for i, v in data.items():
if '/' in i:
parts = i.split('/', 1)
data[parts[0]] = {parts[1]: v}
data.pop(i)
jsonFile.write(json.dumps(data, indent=4))
You can use something like this:
# https://www.geeksforgeeks.org/convert-csv-to-json-using-python/
import csv
import json
# Function to convert a CSV to JSON
# Takes the file paths as arguments
def make_json(csvFilePath, jsonFilePath):
# create a dictionary
data = {}
# Open a csv reader called DictReader
with open(csvFilePath, encoding='utf-8') as csvf:
csvReader = csv.DictReader(csvf)
# Convert each row into a dictionary
# and add it to data
for rows in csvReader:
# Assuming a column named 'No' to
# be the primary key
key = rows['No']
data[key] = rows
# Open a json writer, and use the json.dumps()
# function to dump data
with open(jsonFilePath, 'w', encoding='utf-8') as jsonf:
jsonf.write(json.dumps(data, indent=4))
# Driver Code
# Decide the two file paths according to your
# computer system
csvFilePath = r'Names.csv'
jsonFilePath = r'Names.json'
# Call the make_json function
make_json(csvFilePath, jsonFilePath)
For more information check out https://www.geeksforgeeks.org/convert-csv-to-json-using-python/

How add newline between JSON objects?

I want to add a new dictionary to a JSON file each time i run the code and then read and work with that data, but it's written like this on one line:
{"userid": "person_1", "password": "67890", "name": "person1", "age": "21", "email": "gmail"}{"userid": "person_2", "password": "12345", "name": "person2", "age": "19", "email": "gmail"}
I want to write each dictionary in a newline or separate them in another way because I can't read them and convert them to a python dictionary in this way, here is the code:
To write on json file:
users = {"userid": username, "password": password,
"name": name, "age": age, "email": email}
with open("users.json", "a") as f:
json.dump(users, f)
and to read them, it gives me an error:
with open("users.json",'r') as f:
usersinfo = json.load(f)
Write each json to a new line
with open("users.json", "a") as f:
f.write(json.dumps(users) + '\n')

Python TypeError: '_csv.writer' object is not iterable

I am parsing json to csv. But am getting error as below:
for i in data:
TypeError: '_csv.writer' object is not iterable
Code:
import json
import csv
with open("Data.json", 'r') as file:
data = json.load(file)
CSV_File = 'Data.csv'
with open(CSV_File, 'w') as file:
data = csv.writer(file)
data.writerow([])
for i in data:
data.writerow([])
Data
{
"id": "kljhfksdhkhd",
"name": "BOB",
"birthday": "08/03/1993",
"languages": [
{
"id": "106059522759137",
"name": "English language"
},
{
"id": "107617475934611",
"name": "Telugu language"
},
{
"id": "112969428713061",
"name": "Hindi"
},
{
"id": "343306413260",
"name": "Tamil language"
},
{
"id": "100904156616786",
"name": "Kannada"
}
],
"games": {
"data": [
{
"name": "Modern Combat",
"id": "12134323",
"created_time": "2019-02-21T18:39:41+0000"
},
{
"name": "Cards",
"id": "343232",
"created_time": "2011-06-01T11:13:31+0000"
},
{
"name": "Shuttle Badminton",
"id": "43214321",
"created_time": "2011-06-01T11:13:31+0000"
},
{
"name": "Carrom",
"id": "49y497",
"created_time": "2011-06-01T11:13:31+0000"
},
{
"name": "Chess",
"id": "0984080830",
"created_time": "2011-06-01T11:13:31+0000"
}
],
"paging": {
"cursors": {
"before": "dkkskd",
"after": "dlldlkd"
}
}
}
}
First off, the name data has been assigned to two different objects. Python permits this each assignment overwrites the previous. In the code, data is initially the data from the json file, then a csv.writer instance. A sensible improvement, therefore, is to name the writer writer, and change the code accordingly:
import json
import csv
with open("Data.json", 'r') as file:
data = json.load(file)
CSV_File = 'Data.csv'
with open(CSV_File, 'w') as file:
writer = csv.writer(file)
writer.writerow([])
for i in data:
writer.writerow([])
Now let's deal with how we are writing to the file. writer.writerow expects a list, but writing an empty list: writer.writerow([]) isn't very useful. Probably you want to write the json data to the csv file, so leet's get rid of the empty lists, and indent the writing loop so that it's inside the with block (otherwise the file will be closed).
import json
import csv
with open("Data.json", 'r') as file:
data = json.load(file)
CSV_File = 'Data.csv'
with open(CSV_File, 'w') as file:
writer = csv.writer(file)
for row in data:
writer.writerow(row)
This will work if the json data is a list of lists, that is it looks like this:
[[...], [...], [...], ...]
because each element of the outer list is a list, so iterating over it (for row in data:) yields a list, which writer.writerow can handle. However it's not uncommon for json data to be in the form of a dictionary:
{"k1": [....], "k2": [...], "k3": [...], ...}
In this case, you might want to iterate over the dictionary's values, if they are list:
for row in data.values():
writer.writerow(row)
Finally, the json may be an irregular mix of lists and dictionaries, and may be arbitrarily nested. It's up to you to determine how to map nested json data to the flat csv format.

Converting a pipe delimited CSV file to a JSON file in a certain format

I am trying to convert from a CSV file each row into JSON format.
When I convert it to JSON it includes the square brackets at the beginning and the end, how can it be omitted?. Also I am looking for a way to split piped values into a list of separate hobbies.
This is what I am getting as output:
[
{
"Name": "John",
"Age": "23",
"Hobby": "Kayaking|Football",
"Location": "Miami",
"Profession": "Sales",
},
{
"Name": "Peter",
"Age": "35",
"Hobby": "Football|Basketball|Swimming",
"Location": "Turin",
"Profession": "Mechanic",
},
{
"Name": "James",
"Age": "50",
"Hobby": "Golf",
"Location": "Berlin",
"Profession": "Accountant",
}
]
My desired output
{
"Name": "John",
"Age": "23",
"Hobby": ["Kayaking","Football"],
"Location": "Miami",
"Profession": "Sales",
},
{
"Name": "Peter",
"Age": "35",
"Hobby": ["Football","Basketball","Swimming"],
"Location": "Turin",
"Profession": "Mechanic",
},
{
"Name": "James",
"Age": "50",
"Hobby": "Golf",
"Location": "Berlin",
"Profession": "Accountant",
}
My code:
import glob
import os
import csv
import json
if __name__ == '__main__':
csv.register_dialect('piper', delimiter='|', quoting=csv.QUOTE_NONE)
for filename in glob.glob('path_to_csv\file.csv'):
csvfile = os.path.splitext(filename)[0]
jsonfile = 'jsfile.json'
fieldnames = ("Name","Age","Hobby","Location", "Profession")
with open(csvfile+'.csv') as f:
reader = csv.DictReader(f,fieldnames)#, dialect='piper')
rows = list(reader)
with open(jsonfile, 'w') as f:
json.dump(rows, f, sort_keys=True, indent=2, separators=(',', ': '))
f.write('\n')
rows = list(reader)
There is no need to wrap it into the list since it is already a list. Removing this line will fix the brackets.
for row in reader:
row['Hobby'] = list(row['Hobby'].split('|'))
We need to split the string of hobbies and convert them to list

Categories

Resources