0

How to get json response in javascript variable?

This is my index.json.jbuilder file

`json.array! @properties do |p|
  json.id p.id
  json.Space_name p.Space_name
  json.address p.address
  json.city p.city
  json.state p.state
  json.country p.country
  json.latitude p.latitude
  json.longitude p.longitude
end`

Now i am getting the json response like this

   [{"id":22,"Space_name":"mumbai","address":"mumbai","city":"sharjah","state":"fujairah","country":"United Arab Emirates","latitude":"19.0759837","longitude":"72.87765590000004"},{"id":2,"Space_name":"Bangalore","address":"Banglore","city":"abu-dhabi","state":"ajman","country":"United Arab Emirates","latitude":"37.2809455","longitude":"49.59241339999994"}

but i need output like this(stored in one variable) ,for eg:

properties= [{"id":22,"Space_name":"mumbai","address":"mumbai","city":"sharjah","state":"fujairah","country":"United Arab Emirates","latitude":"19.0759837","longitude":"72.87765590000004"},{"id":2,"Space_name":"Bangalore","address":"Banglore","city":"abu-dhabi","state":"ajman","country":"United Arab Emirates","latitude":"37.2809455","longitude":"49.59241339999994"}

Here i am using jbuilder gem for json respose. Any help is appreciatable.

SreRoR
  • 1,151
  • 1
  • 17
  • 44

1 Answers1

1

Not worked with jbuilder,but I suppose this should work.

#In controller

@properties_json = 'json.array! @properties do |p|
  json.id p.id
  json.Space_name p.Space_name
  json.address p.address
  json.city p.city
  json.state p.state
  json.country p.country
  json.latitude p.latitude
  json.longitude p.longitude
end'

#In the View( *.html)

<script type="text/javascript">
    var properties = <%= @properties_json %>;
</script>
Prashanth
  • 241
  • 1
  • 7