1

I'm trying to find a way to sort JSON data in ruby. It's stored as a fairly complicated (I think so anyway) hash that looks like this:

{
    "allergies": {
        "allergy": [
            {
                "id": "11426793",
                "name": "Milk",
                "category": "Food Allergy",
                "createdPerson": "AGUDELO-HERNANDEZ  ARCADIO",
                "onsetDate": "2014-05-05T00:29:28-04:00"
            }, {
                "id": "11426788",
                "name": "Antibiotics",
                "category": "Drug Allergy",
                "createdPerson": "Smith John H",
                "onsetDate": "2014-05-04T22:29:28-04:00"
            }
        ]
    },
    "responseErrors": {
        "responseError": []
    }
}

There's a lot more "Allergy" objects that exist in the actual data, and I want to be able to sort them by the "onsetDate" then by "name". I've tried:

sorted = @allergies["allergies"].sort_by { |hsh| hsh["name"] }

but I get the following error:

no implicit conversion of String into Integer (TypeError)

2 Answers 2

1

You were almost there:

require "json"

str = <<EOS 
 {
  "allergies": {
    "allergy": [
    {
      "id": "11426793",
      "name": "Milk",
      "category": "Food Allergy",
      "createdPerson": "AGUDELO-HERNANDEZ  ARCADIO",
      "onsetDate": "2014-05-05T00:29:28-04:00"
   },
   {
     "id": "11426788",
     "name": "Antibiotics",
     "category": "Drug Allergy",
     "createdPerson": "Smith John H",
     "onsetDate": "2014-05-04T22:29:28-04:00"
   }

]
},
"responseErrors": {
  "responseError": []
}
}
EOS

@allergies = JSON.parse(str)
puts @allergies["allergies"]["allergy"].sort_by { |hsh| hsh["name"] }

You have JSON (not hash), so you first have to parse it from JSON into hash. Also, the array you are trying to sort is 2 levels deep.

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

Comments

0

@allergies["allergies"] returns a hash, which is:

{
    "allergy": [
        {
            "id": "11426793",
            "name": "Milk",
            "category": "Food Allergy",
            "createdPerson": "AGUDELO-HERNANDEZ  ARCADIO",
            "onsetDate": "2014-05-05T00:29:28-04:00"
        }, {
            "id": "11426788",
            "name": "Antibiotics",
            "category": "Drug Allergy",
            "createdPerson": "Smith John H",
            "onsetDate": "2014-05-04T22:29:28-04:00"
        }
    ]
}

Calling sort_by on a hash, it will yield each key-value pairs to the block. So in

@allergies["allergies"].sort_by { |hsh| hsh["name"] }

hsh is an array. Array#[] expects Integer as its argument. It will try to convert "name" to Integer, and fails with the error you seen.

You need

@allergies["allergies"]["allergy"].sort_by { |hsh| hsh["name"] }

or, if you want to sort the values in place, so that the original data will have sorted allergy, use sort_by!

Comments

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.