How do I add JSON data in the child table against the parent table in Ruby on Rails? -


i have been struggling in ruby on rails.

i have 4 tables interlinked: a, b, c, , d. a parent b , b parent c , d.

i have records existing in table b , want add multiple entries against particular record, example "3", in 'c' , 'd' tables against id.

the data format is:

[{\"waypoint\":{\"latitude\":37.3645616666667,\"timestamp\":\"2012-10-16t09:58:50z\",\"background\":false,\"estimated_speed\":17.4189262390137,\"journey_id\":null,\"longitude\":-112.850676666667}},{\"waypoint\":{\"latitude\":37.3648733333333,\"timestamp\":\"2012-10-16t09:58:54z\",\"background\":false,\"estimated_speed\":17.076057434082,\"journey_id\":null,\"longitude\":-112.85077}},{\"waypoint\":{\"latitude\":37.3651116666667,\"timestamp\":\"2012-10-16t09:58:57z\",\"background\":false,\"estimated_speed\":15.4269437789917,\"journey_id\":null,\"longitude\":-112.850766666667}},{\"waypoint\":{\"latitude\":37.36547,\"timestamp\":\"2012-10-16t09:59:02z\",\"background\":false,\"estimated_speed\":17.1007328033447,\"journey_id\":null,\"longitude\":-112.85072}},{\"waypoint\":{\"latitude\":37.3658433333333,\"timestamp\":\"2012-10-16t09:59:11z\",\"background\":false,\"estimated_speed\":10.3052024841309,\"journey_id\":null,\"longitude\":-112.850738333333}}]"    

i data web service. see journey_id null, whereas want 3, want make entry against id.

how can save data in child table using id?

your json string isn't opened correctly in sample, it's missing leading '"'. fixing , moving on, here's json looks "prettified":

 [   {     "waypoint": {       "latitude": 37.3645616666667,       "timestamp": "2012-10-16t09:58:50z",       "background": false,       "estimated_speed": 17.4189262390137,       "journey_id": null,       "longitude": -112.850676666667     }   },   {     "waypoint": {       "latitude": 37.3648733333333,       "timestamp": "2012-10-16t09:58:54z",       "background": false,       "estimated_speed": 17.076057434082,       "journey_id": null,       "longitude": -112.85077     }   },   {     "waypoint": {       "latitude": 37.3651116666667,       "timestamp": "2012-10-16t09:58:57z",       "background": false,       "estimated_speed": 15.4269437789917,       "journey_id": null,       "longitude": -112.850766666667     }   },   {     "waypoint": {       "latitude": 37.36547,       "timestamp": "2012-10-16t09:59:02z",       "background": false,       "estimated_speed": 17.1007328033447,       "journey_id": null,       "longitude": -112.85072     }   },   {     "waypoint": {       "latitude": 37.3658433333333,       "timestamp": "2012-10-16t09:59:11z",       "background": false,       "estimated_speed": 10.3052024841309,       "journey_id": null,       "longitude": -112.850738333333     }   } ] 

you have array of waypoint objects. parsing json ruby object:

obj = json["[{\"waypoint\":..."] # purposely truncated brevity 

returns array of hashes:

 [{"waypoint"=>    {"latitude"=>37.3645616666667,     "timestamp"=>"2012-10-16t09:58:50z",     "background"=>false,     "estimated_speed"=>17.4189262390137,     "journey_id"=>nil,     "longitude"=>-112.850676666667}},  {"waypoint"=>    {"latitude"=>37.3648733333333,     "timestamp"=>"2012-10-16t09:58:54z",     "background"=>false,     "estimated_speed"=>17.076057434082,     "journey_id"=>nil,     "longitude"=>-112.85077}},  {"waypoint"=>    {"latitude"=>37.3651116666667,     "timestamp"=>"2012-10-16t09:58:57z",     "background"=>false,     "estimated_speed"=>15.4269437789917,     "journey_id"=>nil,     "longitude"=>-112.850766666667}},  {"waypoint"=>    {"latitude"=>37.36547,     "timestamp"=>"2012-10-16t09:59:02z",     "background"=>false,     "estimated_speed"=>17.1007328033447,     "journey_id"=>nil,     "longitude"=>-112.85072}},  {"waypoint"=>    {"latitude"=>37.3658433333333,     "timestamp"=>"2012-10-16t09:59:11z",     "background"=>false,     "estimated_speed"=>10.3052024841309,     "journey_id"=>nil,     "longitude"=>-112.850738333333}}] 

you can walk through array , access, or change, value journey_id:

row = 3 obj = obj.map{ |h| h['waypoint']['journey_id'] = row } obj.first 

looking @ first hash shows value changed, rest:

{   "waypoint" => {     "latitude" => 37.3645616666667,     "timestamp" => "2012-10-16t09:58:50z",     "background" => false,     "estimated_speed" => 17.4189262390137,     "journey_id" => 3,     "longitude" => -112.850676666667   } } 

at point, need recreate json string. can figure out reading the json documentation.

you modifying received string directly, don't want habit of directly modifying json strings because can inadvertently damage payload. it's better let parser give structure, modify that, let json recreate string.

how store database left exercise also.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -