Home convert deeply nested JSON into OpenStruct object
Post
Cancel

convert deeply nested JSON into OpenStruct object

This post will help you convert JSON objects into an OpenStruct object, which will allow us to extract value directly using . dot notation e.g: json_result.key

If you are using with any JSON API services, than you might have come across a situation where you have to extract certain value, check if it exists in JSON response or not and doing that in traditional way is too much pain

1
2
3
4
5
6
7
8
9
{
  "name": "John doe",
  "email": "john@example.com",
  "address": {
    "primary": {
      "city": "Pokhara"
    }
  }
}

Now obviously, we want to extract a value of city but for that, we need to go with the pain of verifying each parent keys to have a child result like so:

1
2
3
4
5
6
7
8
9
def city(json)
  return if json['address'].blank?
  return if json['address']['primary'].blank?

  json['address']['primary']['city']
end

city(json)
=> "Pokhara"

JSON to OpenStruct object

we can simplyfy this by using Safe navigation operator and OpenStruct class.

1
2
3
res = JSON.parse(json, object_class: OpenStruct)
res.address&.primary&.city
=> "Pokhara"

Hash to OpenStruct object

we can also convert hash to openstruct object by converting hash into JSON using .to_json method:

1
2
3
res = JSON.parse(hsh.to_json, object_class: OpenStruct)
res.address&.primary&.city
=> "Pokhara"

More info

This post is licensed under CC BY 4.0 by the author.