Mani
Mani

Reputation: 2563

Access only one attribute of API in response - Ruby on Rails 4

I am calling an API that returns an array of JSON objects and I can access return values of the API call

[{"param1":1,"param2":"blah1"},
{"param1":2,"param2":"blah2"},
{"param1":3,"param2":"blah3"}]

I know that i can access each Param1 through iteration or by static indexing like @client[0].param1 @client[1].param1 @client[2].param1 but the thing is , i don't want param2 and i want just param1 . is there any way , to access param1 without iteration or static indexing so that i could get the below result in response

[{"param1":1},
{"param1":2},
{"param1":3}]

Update

The thing to notice is that i want to filter the result while making the request (before getting the response when we know the attribute name)

Upvotes: 0

Views: 111

Answers (1)

akbarbin
akbarbin

Reputation: 5105

Try to use delete.

Deletes and returns a key-value pair from hsh whose key is equal to key. If the key is not found, returns the default value. If the optional code block is given and the key is not found, pass in the key and return the result of block.

data = [{"param1":1,"param2":"blah1"},
{"param1":2,"param2":"blah2"},
{"param1":3,"param2":"blah3"}]

data.each {|x| x.delete("param2")}

For more information about delete. I hope this help you.

Upvotes: 0

Related Questions