I have a JSON file the has a structure something like this:
[
{
"Name" : {
"Attribute" : " Value",
"Attribute2" : " Value2",
"Attribute3" : " Value3",
}
, "Name2" : {
...
}
]
I'm trying to seed this file into a database table. I don't need all attribute:value pairs, so I need to map the ones I need to the create command in the loop. This is what I've attempted in the seeds.rb file:
json = ActiveSupport::JSON.decode(File.read("db/exercises.json"))
json.each_with_index do |e, index|
Model.create!(
name: e[0]
)
end
What I essentially need to do is something like this:
Model.create!(
name: e[0],
attribute1: e[0][attribute1],
attribute3: e[0][attribute3]
)
Any help ?
JSON.parse, notdecode, and you shouldn't be referencinge[0]-eis not an array.{ ... repeat ... }supposed to be the second element in the top-level array, rather than a sibling of"Name"(which is invalid syntax)?