0

I am a beginner with Ruby, and I have the following Json array:

    "elements": [
        {
          "type": "Contact",          
          "id": "1",
          "createdAt": "131231235",
          "name": "test",
          "updatedAt": "1456328049",
          "accountName": "Mr Test",
          "country": "China",
          "firstName": "Test",
          "lastName": "lastNameTest",
        },
        {
          "type": "Contact",          
          "id": "2",
          "createdAt": "156453447",
          "name": "test2",
          "updatedAt": "124464554",
          "accountName": "Mr Test2",
          "country": "Germany",
          "firstName": "Test2",
          "lastName": "lastNameTest2",
            },...
]

I want to filter out only a few keys + values: for example I want to return only the id,name,accountName,firstname and lastname.

So the exspected output is the following:

    "elements": [
        {         
          "id": "1",
          "name": "test",
          "accountName": "Mr Test",
          "firstName": "Test",
          "lastName": "lastNameTest",
        },
        {      
          "id": "2",
          "name": "test2",
          "accountName": "Mr Test2",
          "firstName": "Test2",
          "lastName": "lastNameTest2",
            },...
]

I tried the following: create a filter array which has the elements I want to return and then map over the items but then I get stuck..

filters = []
filters.push("accountName")
filters.push("lastName")
filters.push("firstName")
filters.push("Id")

output["elements"].each do |item|
result = []
item.map {|key,value|filters.include? key}
result.push(?)

Thank you for the help.

M. Suurland
  • 725
  • 12
  • 31
  • 1
    A very important thing to understand is that JSON is merely a serialization mechanism, and when manipulating objects *expressed as JSON*, you should deserialize them (using JSON.parse), and operate on whatever Ruby objects are created. Then, if you need JSON, you can use #to_json or JSON.pretty_generate to create the modified JSON. So it's best to think "JSON *representation* of an array*" and not "JSON array". – Keith Bennett Apr 25 '16 at 13:41

1 Answers1

2

Check this out, you should be able to work out from this:

output = { "elements": [
          {
            "id": "1",
            "name": "test",
            "accountName": "Mr Test",
            "firstName": "Test",
            "lastName": "lastNameTest",
            "somethoong": "sdsad"
          },
          {
            "id": "2",
            "name": "test2",
            "accountName": "Mr Test2",
            "firstName": "Test2",
            "lastName": "lastNameTest2"
            }
          ]}

attribs = %w(accountName lastName firstName id)
output[:elements].each do |item|
  item.delete_if{|k,v| !attribs.include?(k.to_s)}
end
Alfie
  • 2,706
  • 1
  • 14
  • 28
  • Thanks @Alfie, not really familiair yet with the methods ruby offers. – M. Suurland Apr 25 '16 at 10:21
  • output[:elements].each do |item| item.keep_if{|k,_| %i(accountName lastName firstName id).include? k } end – hirolau Apr 25 '16 at 11:14
  • Also, if you are using Rails, you can `output["elements"].slice("accountName", "lastName", "firstName", "id")`. Turns out `slice` is now included in Ruby 2.5 – https://bugs.ruby-lang.org/issues/8499 – codenamev Aug 08 '18 at 15:49