2

I may be doing multiple things wrong here. Very new to python and JSON. I have multiple "song"-JSON objects. Which I need to write and read from a file. The JSON File looks like this (basically a list of song objects, NOT one per line! here only two):

[{
    "emotions": [],
    "lyrics": "2222",
    "emotionID": 0,
    "artist": "22453232",
    "sentimentScore": 0,
    "subjects": [],
    "synonymKeyWords": [],
    "keyWords": []
}, {
    "emotions": [],
    "lyrics": "244422",
    "emotionID": 0,
    "artist": "2121222",
    "sentimentScore": 0,
    "subjects": [],
    "synonymKeyWords": [],
    "keyWords": []
}]

I want to read the song objects into a list so that I can append another song object and then write it back. What I have is obviously wrong. Help please.

import json
from song import Song
def writeToFile():
    lyrics = input( "enter lyrics: " )
    artist = input("enter artist name: ")
    songObj = Song(lyrics, artist)
    print(vars(songObj))
    data = []
    with open('testWrite.json') as file:
        data = json.load(file)
        data.append(vars(songObj))
        print(data)
    with open('testWrite.json', 'w') as file:
        json.dump(data, file) 

ctr = "y"
while (ctr=="y"):
    writeToFile()
    ctr = input("continue? y/n?")

Open to other suggestions as well, if I can avoid loading all the objects for every time I want to append a new song object.

2 Answers 2

2

I think you have a couple issues going on here. First, valid JSON doesn't use single quotes ('), it is all double quotes ("). You are looking for something like:

[{
"id":123,
"emotions":[],
"lyrics":"AbC",
"emotionID":0,
"artist":"222",
"sentimentScore":0,
"subjects":[],
"synonymKeyWords":[],
"keyWords":[]
},

{
"id":123,
"emotions":[],
"lyrics":"EFG",
"emotionID":0,
"artist":"223",
"sentimentScore":0,
"subjects":[],
"synonymKeyWords":[],
"keyWords":[]
}
]

Secondly, you need to open the json file for reading and then load it as json. The following should work for you:

with open(read_file) as file:
  data = json.load(file)

with open(write_file, 'w') as file:
  json.dump(data, file)

print(data)
Sign up to request clarification or add additional context in comments.

1 Comment

hey, thanks. I have edited the question, json file and code above. Please check. I am getting the error : UnsupportedOperation: not writable I think it is reading but still unable to write back. (I need to write back to the same file.
0
data.append(json.loads(f))

This appends the list you read from the JSON file as a single element to the list. So after your other append, the list will have two elements: One list of songs, and that one song object you added afterwards.

You should use list.extend to extend the list with the items from another list:

data.extends(json.loads(f))

Since your list is empty before that, you can also just load the list from the JSON and then append to that one:

data = json.loads(f)
data.append(vars(songObj))

3 Comments

hey, thanks. I have edited the question, json file and code above. Please check. I am getting the error : UnsupportedOperation: not writable I think it is reading but still unable to write back. (I need to write back to the same file.
open('testWrite.json') just opens the file in read-only mode, if you want to write to it, you need to use e.g. the w mode: open('testWrite.json', 'w').
okay! I did that. Now no error, but no change to the test.Write.json file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.