What is wrong with my JSON? I am running on Rails 6, and I am loading the file contents, after I load the file, I try to parse it. However, I am running into parsing errors.
This is my code:
file_name = File.join(Rails.root, "lib", "data", "cars.json") file_content = File.read(file_name) json_plans = JSON.parse file_content Error:
JSON::ParserError (784: unexpected token at '{)
JSON:
{ "first_car": { name: "first car", plans: [ { "amount": 0, "stripe_id": "" , "name": "first car", "published": true, "interval": "year", "interval_count": "1", "category": "car", "slug": "first_car" } ] }, "second_car": { name: "second car", plans: [ { "amount": 1000, "stripe_id": "", "name": "second car", "published": true, "interval": "year", "interval_count": "1", "category": "car", "slug": "second_car" } ] }, "third_car": { name: "third car", plans: [ { "amount": 20000, "stripe_id": "", "name": "third car", "published": true, "interval": "year", "interval_count": "1", "category": "car", "slug": "third_car" } ] } } 1 Answer
You need to put name property as string wrapped in ""
The output is not very useful though, I agree.
Here's a correct JSON
{ "first_car": { "name": "first car", "plans": [ { "amount": 0, "stripe_id": "" , "name": "first car", "published": true, "interval": "year", "interval_count": "1", "category": "car", "slug": "first_car" } ] }, "second_car": { "name": "second car", "plans": [ { "amount": 1000, "stripe_id": "", "name": "second car", "published": true, "interval": "year", "interval_count": "1", "category": "car", "slug": "second_car" } ] }, "third_car": { "name": "third car", "plans": [ { "amount": 20000, "stripe_id": "", "name": "third car", "published": true, "interval": "year", "interval_count": "1", "category": "car", "slug": "third_car" } ] } }