In Rails, how do I access the individual elements of this json? I parsed it using ruby’s .to_json method.json = CSV.parse(csv).to_json.
Here’s what is returned:
[
[
"id",
"subject"
],
[
"1",
"Economics"
],
[
"2",
"General Paper"
],
[
"3",
"History"
],
[
"4",
"Geography"
],
[
"5",
"Mathematics"
],
[
"6",
"Chemistry"
],
[
"7",
"Biology"
],
[
"8",
"Physics"
]
]
Trying to access json[0][1] or json[0] returns "["
I have a feeling it has to do with the square brackets but I’m sure there’s a way to access it (It looks like arrays in an array to me and that means of access should work)
When you convert the data to JSON it becomes a JSON string. If you access it as an array you’ll just get the character item referenced.
If you just want access to the data drop the to_json bit. The CSV parse should give you back the array of arrays and you can access it how you want.