2

I am attempting to write a program that takes user input and write it to a JSON file to be read later. Before doing so, I take the users input and create a hash and convert it to JSON format before writing it to a file. My problem arises when I have more than one JSON object that needs to be read from the file.

Here is my current code to create a hash and move data into a file and the method I use to read from the file.

#create hash of user input
tempHash = {
:first_name => $user_fname, 
:last_name => $user_lname,
:raw_date => input_date,
:birthday => $user_bday
}

#open birthday_output.json file and write the JSON object to file
File.open("#{@filePath}/birthday_output.json", 'a+') do |file|
    file.puts JSON.pretty_generate(tempHash)
    file.close
end

#read the JSON object from file and return value of specific key
def file_reader(key)
    file = File.read("#{@filePath}/birthday_output.json")
    data_hash = JSON.parse(file)
    return data_hash[key]
end

This all works great when I have only 1 JSON object in the file that I am attempting to read, but when I create another JSON object and write it to the file, I get the error listed below when attempting to parse the file. My goal is to create a file with user input and then read over those keys/values to see if the user input already exists in the file.

The error that I am recieving is

"in 'parse': 776: Unexpected token at ' JSON Parser error
"first_name": "Jon"
"last_name": "Stewart"
"raw_date": "2016-06-11"
"birthday": "June 11 2016"

My file looks like this when I get the error:

{
"first_name": "Hank",
"last_name": "Hill",
"raw_date": "2016-01-05",
"birthday": "January 05 2016"
}
{
"first_name": "Jon",
"last_name": "Stewart",
"raw_date": "2016-06-11",
"birthday": "June 11 2016"
}    

Again, when only the Hank Hill JSON object exists in the file, I am able to call my file_reader method with the key that I am interested in and it outputs correctly. When I introduce the second JSON object to the file "Jon Stewart", it launches the error at me. Does anyone have any experience is what I am trying to do?

1
  • 1
    I would recommend not using JSON if you're writing to disk. While it's useful for serializing data across the wire, if you're writing configuration information I'd use YAML which is much more readable. If you're going to be treating the data as a small database, then I'd recommend using a real database. If it's small use SQLite. If it will grow use MySQL or PostgreSQL. Commented Jan 10, 2016 at 23:03

1 Answer 1

1

I would suggest making the file contain a list of json objects so that it would be

[
  {
    "first_name": "Hank",
    "last_name": "Hill",
    "raw_date": "2016-01-05",
    "birthday": "January 05 2016"
  },
  {
    "first_name": "Jon",
    "last_name": "Stewart",
    "raw_date": "2016-06-11",
    "birthday": "June 11 2016"
  }
]

This way you have a valid JSON list in your file and you can just let the json parser do all the work. You don't want to be figuring out manually where the objects end because that would require you to write at least a subset of a json parser.

You will need to update the logic in your code to do things like add the newest entry to the end of the list and then dump the object to the file as json instead of just writing the object to the end of the file.

Sign up to request clarification or add additional context in comments.

Comments

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.