2

I am trying to extract elements from this API response, but I am not able to for some reason. I have the following API response body: `

[
  {
    "ID": "295699",
    "restriction": [
      {
        "restrictionTypeCode": "10001"
      }
    ]
  }
]

` Now, I simply want to print restrictionTypeCode

  json_string = RestClient.get "#{$uri}", {:content_type => 'application/json'}
  hash = JSON.parse(json_string) 
  code= hash['restriction']['restrictionTypeCode']
  puts code

the above code errors out and it doesn't display the restrictionTypeCode

3
  • What error is raised? Which line from? Could you paste the stacktrace? Commented Sep 23, 2019 at 10:13
  • Thank you @mrzasa. I updated your code, but now I am getting Nil values in the console $uri = "URL" $json_string = RestClient.get "#{$uri}", {:content_type => 'application/json'} $hash = JSON.parse($json_string) puts $hash.first&.dig(:restriction)&.first&.dig(:restrictionTypeCode) puts $hash.flat_map { |hsh| hsh[:restriction]&.map { |sub_hsh| sub_hsh[:restrictionTypeCode] } } . When I output the $json_string or the hash I can see the entire response. But when I output the 2 lines that you provided, I get empty 2 lines in the console for both "puts" above Commented Sep 23, 2019 at 16:34
  • it's not me, it's @SRack that helps you :) Commented Sep 23, 2019 at 18:24

1 Answer 1

3

Your problem is your data is returning arrays in places. Try the following:

data = [
  {
    "ID": "295699",
    "restriction": [
      {
        "restrictionTypeCode": "10001"
      }
    ]
  }
]

data.first[:restriction].first[:restrictionTypeCode]
# to make this safe from any nil values you may encounter, you might want to use
data.first&.dig(:restriction)&.first&.dig(:restrictionTypeCode)
# => "10001"

# or 

data.flat_map { |hsh| hsh[:restriction]&.map { |sub_hsh| sub_hsh[:restrictionTypeCode] } }
# => ["10001"]

To break it down a little, your top level response and that falling under the key :restriction both return arrays; therefore, to get the data from them you either need to access one of the items they contain (in my example using first) or map them (the second example).

I've added some checks for nil values in there: this is pretty vital when dealing with API responses as you're not in control of the data so can't be certain all the fields will be present. Rather than throw an error should you encounter data like this, you'll get nil returned to avoid breaking subsequent code.

Hope this helps - let me know how you get on or if you have any questions :)

Sign up to request clarification or add additional context in comments.

4 Comments

hash.first['restriction'].first['restrictionTypeCode']
Also most probably symbols, not strings as keys
Thanks @mrzasa - good catch. I'll update. Much appreciated :)
How did you get on with this @DeanE?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.