'To fetch elements and compare elements from JSON object in Ruby

I have many json resources similar to the below one. But, I need to only fetch the json resource which satisfies the two conditions: (1) component.code.text == Diastolic Blood Pressure (2) valueQuantity.value < 90

This is the JSON object/resource

      
     {
    "fullUrl": "urn:uuid:edf9439b-0173-b4ab-6545 3b100165832e",
    "resource": {
      "resourceType": "Observation",
      "id": "edf9439b-0173-b4ab-6545-3b100165832e",
      "component": [ {
        "code": {
          "coding": [ {
            "system": "http://loinc.org",
            "code": "8462-4",
            "display": "Diastolic Blood Pressure"
          } ],
          "text": "Diastolic Blood Pressure"
        },
        "valueQuantity": {
          "value": 81,
          "unit": "mm[Hg]",
          "system": "http://unitsofmeasure.org",
          "code": "mm[Hg]"
        }
      }, {
        "code": {
          "coding": [ {
            "system": "http://loinc.org",
            "code": "8480-6",
            "display": "Systolic Blood Pressure"
          } ],
          "text": "Systolic Blood Pressure"
        },
        "valueQuantity": {
          "value": 120,
          "unit": "mm[Hg]",
          "system": "http://unitsofmeasure.org",
          "code": "mm[Hg]"
        }
      } ]
    },
    
  }

JSON file

I need to write a condition to fetch the resource with text: "Diastolic Blood Pressure" AND valueQuantity.value > 90

I have written the following code:

    def self.hypertension_observation(bundle)
      entries = bundle.entry.select {|entry| entry.resource.is_a?(FHIR::Observation)}
      observations = entries.map {|entry| entry.resource}
      hypertension_observation_statuses = ((observations.select {|observation| observation&.component&.at(0)&.code&.text.to_s == 'Diastolic Blood Pressure'}) && (observations.select {|observation| observation&.component&.at(0)&.valueQuantity&.value.to_i >= 90}))
    end

I am getting the output without any error. But, the second condition is not being satisfied in the output. The output contains even values < 90.

Please anyone help in correcting this ruby code regarding fetching only, output which contains value<90



Solution 1:[1]

I will write out what I would do for a problem like this, based on the (edited) version of your json data. I'm inferring that the full json file is some list of records with medical data, and that we want to fetch only the records for which the individual's diastolic blood pressure reading is < 90.

If you want to do this in Ruby I recommend using the JSON parser which comes with your ruby distro. What this does is it takes some (hopefully valid) json data and returns a Ruby array of hashes, each with nested arrays and hashes. In my solution I saved the json you posted to a file and so I would do something like this:

require 'json'
require 'pp'

json_data = File.read("medical_info.json")

parsed_data = JSON.parse(json_data)

fetched_data = []

parsed_data.map do |record|
    diastolic_text = record["resource"]["component"][0]["code"]["text"]
    diastolic_value_quantity = record["resource"]["component"][0]["valueQuantity"]["value"]
    if diastolic_value_quantity < 90
      fetched_data << record
    end
end 

pp fetched_data

This will print a new array of hashes which contains only the results with the desired values for diastolic pressure. The 'pp' gem is for 'Pretty Print' which isn't perfect but makes the hierarchy a little easier to read.

I find that when faced with deeply nested JSON data that I want to parse in Ruby, I will save the JSON data to a file, as I did here, and then in the directory where the file is, I run IRB so I can just play with accessing the hash values and array elements that I'm looking for.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dharman