How to save data from a json array to the database MongoDB?

How to save the marker coordinates received as json parameter and build it to lat and lng fields with right values? I tried to do this in many ways but it gave everyone fails. Below those that are closest in my opinion good.

#controller: def create @user = received_json = ["{\"lat\":52.38521483550798,\"lng\":16.866459809082016}", "{\"lat\":52.40458894240522,\"lng\":16.856503449218735}", "{\"lat\":52.42605757375466,\"lng\":16.86989303662108}"] # objArray=JSON.parse(received_json.to_json) objArray=ActiveSupport::JSON.decode(received_json.to_json) #example 1 objArray.each do |obj| end # NoMethodError (undefined method `each_pair' for "{\"lat\":52.38521483550798,\"lng\":16.866459809082016}":String): #example 2 objArray.each do |obj, value|>value) end # BSON::InvalidKeyName ({"lat":52.38521483550798,"lng":16.866459809082016} - key must not contain '.'): #example 3 objArray.each do |obj| end # This approach save data but in one field lat in user markers array # Data Saved: {"_id":"4e9eac161e7bf208280002fb","markers":[{"_id":"4e9eac161e7bf208280002fc","lat":"{\"lat\":52.38521483550798,\"lng\":16.866459809082016}"},{"_id":"4e9eac161e7bf208280002fd","lat":"{\"lat\":52.40458894240522,\"lng\":16.856503449218735}"},{"_id":"4e9eac161e7bf208280002fe","lat":"{\"lat\":52.42605757375466,\"lng\":16.86989303662108}"}]} end #model class User include Mongoid::Document field :name, :type => String embeds_many :markers, :inverse_of => :user end class Marker include Mongoid::Document field :lat, :type => Float field :lng, :type => Float embedded_in :user, :inverse_of => :marker end


Try the following code

def create
@user =
received_json = ["{\"lat\":52.38521483550798,\"lng\":16.866459809082016}", "{\"lat\":52.40458894240522,\"lng\":16.856503449218735}", "{\"lat\":52.42605757375466,\"lng\":16.86989303662108}"]
markers = received_json.collect { |attributes| ActiveSupport::JSON.decode(attributes) }
markers.each do |marker|

Category:ruby on rails Time:2011-10-19 Views:2

Related post

  • save data in form of arrays to mysql database using a foreach loop 2012-03-03

    I have a form that submits multiple values using php. code is below: echo "<form action='?ud=".$ud."' method='post'>Name: <input type='text' name='fname' />"; $resultw = mysql_query("SELECT * FROM options where userid='$ud' ORDER BY prior

  • onclick add dropdown list data into a json array 2012-05-01

    I have an dropdown item like this <s:select name="title" list="%{rulemoduleVOList}" headerKey="0" headerValue="--Select--" listKey="id" listValue="name" onchange="showStatCode()" id="moduleList" /> addition to this I placed an button <input

  • Convert a json object data to a json array data? 2014-01-02

    How can I convert a json object data to a json array data? For instance, I have this json object data from the server on my console on firebug, Object { 0={...}, 1={...}, 2={...}, more...} json string, { "items": { "0": { "pub_name": "News", "system_

  • How to save data from an HTML form to a database table in WordPress? 2011-03-13

    I have a WordPress theme, and I'm trying to save data from an HTML form into a database. I made the HTML form and added a "save & close" button which calls a JavaScript function named saveData() that takes the data from the form and sends it to a

  • How can I get data from an Json array in DOJO? 2011-12-19

    I have a small question : If I have a JSON array: { "cate_user_res_id": "4", "from": "url", "mimetype": "image/jpeg", "ext": "jpg", "res_name": "", "user_id": "1", "file_size": 55966, "created": "201

  • getJSON to fetch data from this json array 2012-02-02

    This is a sample json array from my code. How can i use getJSON to fetch data from this array. "Restoration": [ { "Easy": { "value": "1", "info": "This is Easy." }, "Medium": { "value": ".75", "info": "This is Medium." }, "Difficult": { "value": ".5"

  • how to get data table to json array in c# 2012-02-17

    how i can get a json array return from data table in asmx service menthods. am coded like this but am not getting in array [WebMethod(CacheDuration = 500)] [ScriptMethod(ResponseFormat = ResponseFormat.Json, XmlSerializeString = false)] public String

  • How to display data from a JSON array using jQuery 2012-04-04

    How can I get the array data from a JSON sent back by my php script? PHP code: <?php //connects to database include 'connect.php'; $callback = $_GET['callback']; $query = mysql_query("SELECT * FROM users"); $rows = array(); while($r = mysql_fetch_

  • Iterate and ouput conditional data from a json array 2014-09-30

    I have a json file called steps.json which contains this json array that I'd like to iterate over based on the conditions below. { "friends": [ { "firstName" : "Paul", "lastName" : "Taylor", "Step": 2 }, { "firstName" : "Sharon", "lastName" : "Thomas

  • Trying to push data to a JSON array 2011-07-07

    I'm trying create a gantt chart with highcharts and I need to put the data in the JSON format. I'm getting real close, but the problem i'm having is that the data I am pushing is being surrounded by quotes. I'm guessing there is just something that I

  • How to build a JSON array from mysql database 2011-06-08

    Okay I have been racking my brain trying to build a JSON array from mysql. The array MUST be in the following format. I am using fullcalendar and want to make the events on the calendar dynamic. Below is the code that builds the array, but currently

  • Save data from Google Maps markers to a database using WordPress, JavaScript, and PHP 2011-03-12

    I have a WordPress template with a map using Google Maps API v3 I use a .php file to add data into a row in my WordPress database (I have a table, of course) using a JavaScript script I create a Google Maps marker, an infowindow and a table. The scri

  • Null pointer Exception while inserting json array into sqlite database 2011-12-07

    12-07 11:25:34.290: E/AndroidRuntime(9177): FATAL EXCEPTION: AsyncTask #2 12-07 11:25:34.290: E/AndroidRuntime(9177): java.lang.RuntimeException: An error occured while executing doInBackground() 12-07 11:25:34.290: E/AndroidRuntime(9177): at android

  • How to save data received in json format to database? 2011-12-02

    i have an rubyonrails backend for an iphone application the webservice receives data in json format eg: [ {"created_at":"2011-11-28T12:53:25Z","body":"good article","updated_at":"2011-11-23T12:53:30Z","id":1,"commenter":"shanib","user_id":1}, {"creat

  • How to add a data dynamically from JSON array to HTML table using JQuery 2011-12-13

    I have created a HTML table <table id="top_five_table"> <tr> <td> </th> <th>URL</th> <th width="90">Total Hits</th> <th width="380">Percentage of all Hits</th> </tr> <tr> <td

  • Trying to put multiple data in php/json array 2012-01-24

    This is probably quite simple to do.. but I just can't think of how to do this. I have a photo upload script, I want to return two types of data in an array, right now I just have one set of data for the photo returned. right now I do this: $photos =

  • UIPickerView empty with data source as JSON array 2012-03-22

    I'm trying to populate a UIPickerView with JSON data that is parsed into an NSArray. The console shows that the JSON is parsing properly but the UIPickerView is remaining empty. Here's my code: - (NSInteger)numberOfComponentsInPickerView:(UIPickerVie

  • Retrieving data from a JSON sub array in javascript, where identifier starts with an integer 2010-04-25

    I must be missing something simple here, but I'm having trouble retrieving data from a JSON array response. I can access objects with identifiers that start with letters, but not ones that start with numbers. For example, I can access data.item[0].sp

  • Where to save data coming from server in android application? 2012-02-17

    I am creating an social networking kind of application in Android.In which I am getting lots of data from server in XML format. Currently I am saving that data in static List & Maps,so I can use this data wherever I want.But I don't know whether

Copyright (C), All Rights Reserved.

processed in 0.532 (s). 13 q(s)