Answer the question
In order to leave comments, you need to log in
Unexpected token at '' when using JSON in ruby?
Ruby 2.0
json (1.8.1, 1.7.7)
A function that adds an object to a json file:
def add(input_hash)
f = File.open('db.dat','a+')
f.write(JSON.generate(input_hash))
f.close
end
def get
f = File.open('db.dat','a+')
tmp = JSON.parse(f.read)
return tmp
end
/var/lib/gems/2.0.0/gems/json-1.8.1/lib/json/common.rb:178:in `parse': 757: unexpected token at '{"mail":{"google": "password"}}' (JSON::ParserError)
>> JSON.parse([ 'foo' ].to_json).first
=> "foo"
>> json = 'foo'.to_json
>> JSON.parse(json, :quirks_mode => true)
=> "foo"
Answer the question
In order to leave comments, you need to log in
If you write two json objects one after another, you get invalid json, see the contents of the file.
f = File.open('db.dat','a+'); here it is more logical to use 'r'
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question