How to Parse Json With Ruby on Rails

Parse JSON to an Array in Rails

JSON.parse <string> is probably what you need here. It could look something like this:

test "works" do
get :method

result = JSON.parse(response.body)

assert_equal 1, result.count
end

Check out "How to unit-test a JSON controller?" and the ActiveSupport JSON documention.

How to parsing JSON string parameters in rails

You could do something like this with native Ruby JSON library:

@json = JSON.parse(request.body.read)
username = @json['username']
password = @json['password']

You take the POST request data and then parse to JSON.

Parsing a static JSON file into a Rails object

You can try:

@post = Post.new
@post.assign_attributes JSON.parse(File.read('app/assets/javascripts/flickr_feed.json'))

If you use protected_attributes gem, attributes set using this manner must be defined with attr_accessible in the model or parameter without_protection must be used.

Edit:

def index
@posts =
JSON.parse(File.read('app/assets/javascripts/flickr_feed.json'))["posts"].inject([]) do |_posts, post_attrs|
_posts << Post.new(post_attrs)
end
respond_to do |format|
format.html
format.json { render json: @posts }
end
end

parse json to object ruby

The following code is more simple:

require 'json'

data = JSON.parse(json_data)
residents = data['Resident'].map { |rd| Resident.new(rd['phone'], rd['addr']) }

Parse JSON from HTTParty using Ruby on Rails

HTTParty.get returns an encapsulated response type of class HTTParty::Response, you need to retrieve the parsed response from that by:

response = HTTParty.get(
'http://codekyt.in/froodle-wp/wp-json/data/v2/projects?client_key=12345',
:headers =>{'Content-Type' => 'application/json'}
)
@category = response.parsed_response # this will return the json.

Also you do not need to iterate on @category in your case, the json object is singular and you can directly use it:

<%=@category["restaurant_name"]%>
<%=@category["country"]%>
<%=@category["currency"]%>
<%=@category["usd"]%>
<%=@category["client_key"]%>
<%=@category["client_name"]%>
<%=@category["client_email"]%>
<%=@category["client_phone"]%>
<%=@category["product_tier"]%>
<%=@category["brand_logo_large"]%>

Parse JSON with an array in Rails

What you have pasted is not valid JSON. The trailing comma after on each "name" is a problem

"name": "Chris Rivers",

You'll get a LoadError trying to decode this with ActiveSupport::JSON.decode

MultiJson::LoadError: 399: unexpected token at '{"user_id": 1,"name": "Chris Rivers",},{"user_id": 3,"name": "Peter Curley",}]}'

If we clean up the JSON, turning it into something ActiveSupport::JSON.decode can understand

"{\"users\": [{\"user_id\": 1,\"name\": \"Chris Rivers\"},{\"user_id\": 3,\"name\": \"Peter Curley\"}]}"

you'll see there is no issue iterating over each object in "users" (x below is the above JSON string)

[8] pry(main)> ActiveSupport::JSON.decode(x)["users"].map { |user| user["name"] }
=> ["Chris Rivers", "Peter Curley"]


Related Topics



Leave a reply



Submit