In my Rails application I'm sending a complex JSON string that needs to get decoded. That's not the problem I know how.
Now before I implement everything I'm trying to access some example JSON structures to see if I can get to all the variables. The problem is that the names can be variable.
{"configurations" : [
{ "drinks" : [
{"menus" : [
{ "hot" : [
{"id":15,"unit":"0.33", "price":"1", "currency":"Euro", "position": 4},
{"id":15,"unit":"0.33", "price":"1", "currency":"Euro", "position": 6}
] },
{ "cold" : [
{"id":15,"unit":"0.33", "price":"1", "currency":"Euro", "position": 4},
{"id":15,"unit":"0.33", "price":"1", "currency":"Euro", "position": 6}
] },
{ "terminals" : [ {"id" : 4}, {"id": 6}, {"id": 7} ] },
{ "keys" : { "debit" : "on", "credit": "off" } }
] }
] } ] }
Ok, now the following fields are variable: "drinks", "hot", "cold". All the other fields will be called the same.
Now I would like to access every variable in this JSON string after I decoded it. Before implementing it, I tryed a simple JSON:
{"configuration" : [ { "drinks" : [ { "id":15, "unit":"0.33" } ] } ] }
After decoding in rails resulting in
{ "configuration" => [{"drinks" => [{"id" => 15, "unit" => "0.33" }]}]}
Now how can I access for example id
and unit
without using the word "drinks". The solution should also be scalable to the example above.
Some extra information: In the large JSON I should access all the items listed there (the id's) save them to a table and return the new id and then insert it back in the JSON. (explaining why this needs to be done will take an extra page or 4 ^^ ).