Parse JSON to Object Ruby

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']) }

Parsing a JSON string in Ruby

This looks like JavaScript Object Notation (JSON). You can parse JSON that resides in some variable, e.g. json_string, like so:

require 'json'
JSON.parse(json_string)

If you’re using an older Ruby, you may need to install the json gem.


There are also other implementations of JSON for Ruby that may fit some use-cases better:

  • YAJL C Bindings for Ruby
  • JSON::Stream

How to parse a JSON file into a Ruby object

It looks like you are trying to call a class method, but you have an instance method in your class.

Given this, you could try to change this method to self.from_json(file).

Parsing JSON list object to Ruby hash

A JSON Array of Objects becomes a Ruby Array of Hashes. These Hashes can then contain more Arrays and so on.

If you want to access the Hashes, you need to iterate through the Array. If you want to access the groups you need to iterate again.

scripts = JSON.parse(json)

# Iterate through each script
scripts.each { |script|
# Print the script's name.
puts "Script: #{script["name"]}"

# Iterate through the script's groups.
groups = script["groups"]
groups.each { |group|
# Print each group's name.
puts "Group: #{group["name"]}"
}
}

Iterate JSON file into Ruby object array

There isn't an all that pretty way if this is all you have for the class:

JSON.parse(whatevers).map do |whatever|
element = Whatever.new
element.id = whatever['id']
element.name = whatever['name']
element.email = whatever['email']
element
end

However, if you add an index method like:

class Whatever
def []=(name, value)
instance_variable_set("@#{name}", value)
end
end

It gets down to:

JSON.parse(whatevers, object_class: Whatever)

Parsing JSON object in RUBY with a wildcard?

I want to find the 'availableQuantity' value that's greater than 5 and [...] return the line number and the product number.

First problem: your value is not a number, so you can't compare it to 5. You need to_i to convert.

Second problem: getting the line number is easiest with regular expressions. /\d+/ is "any consecutive digits". Combining that...

q.select { |key, value|
value['availableQuantity'].to_i > 5
}.map { |key, value|
[key[/\d+/].to_i, value['productNumber'].to_i]
}
# => [[2, 112], [3, 113]]

Is it possible to convert a JSON string to an object?

You can parse the string into a ruby hash and then turn it into a Mash. Mash provides you with method-like access.

require 'json'
require 'hashie'

hash = JSON.parse json_string
obj = Hashie::Mash.new hash
obj.drawer.stations.tv.header # => "TV Channels"

Update

You can also do it without a 3rd party gem, using ruby's own OpenStruct:

require 'ostruct'
require 'json'

obj = JSON.parse(json_string, object_class: OpenStruct)
obj.drawer.stations.tv.header # => "TV Channels"

how to iterate and retrive just the values from a json object with ruby

assuming you have the object as a string.

require 'json'
json_obj = '{"a" :"1", "b":"2", "c":"3"}'
values = JSON.parse(json_obj).values

will provide you with the array

["1", "2", "3"]

JSON.parse , parses the json string into a ruby object, in this case an instance of a Hash. The Hash class has a method values which returns an array containing the values or each hash entry.



Related Topics



Leave a reply



Submit