Creating Observation with "hasMember" through FHIR API.

I’m trying to create an Observation with hasMember referencing to other Observation. However when I make a POST request it’s resulting in Status 422:

With error: “obs id is null’ failed to validate with reason: valueText: error.not.null”

Can anyone help me with figuring out the create Part, if anyone has worked on a similar usecase of creating Observation with hasMember.

I’m using an Observation from the OpenMRS dev3:

{
    "resourceType": "Observation",
    "id": "8d0f6276-53e1-402d-ae3d-21275a1d8249",
    "status": "final",
    "code": {
        "coding": [
            {
                "code": "161486AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
                "display": "Culture panel"
            },
            {
                "system": "https://cielterminology.org",
                "code": "161486"
            },
            {
                "system": "http://snomed.info/sct/",
                "code": "61594008"
            }
        ],
        "text": "Culture panel"
    },
    "subject": {
        "reference": "Patient/0f583495-46f5-41f4-a2b9-16c38adee55f",
        "type": "Patient",
        "display": "Betty Williams (OpenMRS ID: 100000Y)"
    },
    "encounter": {
        "reference": "Encounter/4ea8bc25-6e71-497f-a9cd-749e3ae9992f",
        "type": "Encounter"
    },
    "effectiveDateTime": "2022-06-17T16:17:38+00:00",
    "issued": "2025-04-17T09:00:39.000+00:00",
    "valueString": "Separate parasites detected from sample., Separate parasites detected from sample., Separate parasites detected from sample., Greenish-yellow matter detected., Separate parasites detected from sample.",
    "hasMember": [
        {
            "reference": "Observation/3499422e-3453-4d2e-8ac3-2c7edb839c78",
            "type": "Observation"
        }
    ]
}

cc: @jofrancisco

Hi @parthfloyd,

Also following-up on conversations about the same here

In OpenMRS core, an observation group is inherently valueless, as its primary purpose is to logically group related observations. Therefore, the valueString should be applied to the child observation that belongs to the grouping (parent) observation.

This requires two separate FHIR REST calls:

  1. First, create the child observation with the actual value.
  2. Then, using the UUID returned from step 1, create the parent/grouping observation that references the child.
1 Like

Thanks @ruhanga I’m did follow these steps as discussed in that discussion & sent two seperate FHIR REST calls:

This is the child Observation that’s on the dev3.openmrs.org server:

{
    "resourceType": "Observation",
    "id": "3499422e-3453-4d2e-8ac3-2c7edb839c78",
    "meta": {
        "versionId": "1744880439000",
        "lastUpdated": "2025-04-17T09:00:39.000+00:00"
    },
    "text": {
        "status": "generated",
        "div": "<div xmlns=\"http://www.w3.org/1999/xhtml\"><table class=\"hapiPropertyTable\"><tbody><tr><td>Id:</td><td>3499422e-3453-4d2e-8ac3-2c7edb839c78</td></tr><tr><td>Status:</td><td>AMENDED</td></tr><tr><td>Category:</td><td> Laboratory </td></tr><tr><td>Code:</td><td>Culture and sensitivity, blood</td></tr><tr><td>Subject:</td><td><a href=\"http://localhost:8080/openmrs/ws/fhir2/R4/Patient/0f583495-46f5-41f4-a2b9-16c38adee55f\">Betty Williams (OpenMRS ID: 100000Y)</a></td></tr><tr><td>Encounter:</td><td><a href=\"http://localhost:8080/openmrs/ws/fhir2/R4/Encounter/4ea8bc25-6e71-497f-a9cd-749e3ae9992f\">Encounter/4ea8bc25-6e71-497f-a9cd-749e3ae9992f</a></td></tr><tr><td>Effective:</td><td> 17 June 2022 16:17:38 </td></tr><tr><td>Issued:</td><td>17/04/2025 09:00:39 AM</td></tr><tr><td>Value:</td><td>Separate parasites detected from sample.</td></tr></tbody></table></div>"
    },
    "status": "amended",
    "category": [
        {
            "coding": [
                {
                    "system": "http://terminology.hl7.org/CodeSystem/observation-category",
                    "code": "laboratory",
                    "display": "Laboratory"
                }
            ]
        }
    ],
    "code": {
        "coding": [
            {
                "code": "161155AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
                "display": "Culture and sensitivity, blood"
            },
            {
                "system": "http://loinc.org",
                "code": "600-7"
            },
            {
                "system": "https://cielterminology.org",
                "code": "161155"
            },
            {
                "system": "http://snomed.info/sct/",
                "code": "273973005"
            }
        ],
        "text": "Culture and sensitivity, blood"
    },
    "subject": {
        "reference": "Patient/0f583495-46f5-41f4-a2b9-16c38adee55f",
        "type": "Patient",
        "display": "Betty Williams (OpenMRS ID: 100000Y)"
    },
    "encounter": {
        "reference": "Encounter/4ea8bc25-6e71-497f-a9cd-749e3ae9992f",
        "type": "Encounter"
    },
    "effectiveDateTime": "2022-06-17T16:17:38+00:00",
    "issued": "2025-04-17T09:00:39.000+00:00",
    "valueString": "Separate parasites detected from sample."
}

However when I tried to post parent observation without the valueString, it’s returning this error:

“HAPI-0389: Failed to call access method: java.lang.NullPointerException: The Type object should not be null”

Let me know if you find different results on reproducing it.

2 Likes

Oh, right — Sorry I just remembered that creating observation groups isn’t actually supported yet in FHIR, as posting Bundles isn’t possible with the current API. It’s been a while since I last worked with the API.

2 Likes

FYI for folks; this is related to this Ticket: https://openmrs.atlassian.net/browse/FM2-661

Great to hear the ICRC team is working on this; sounds like the work is on the ObservationTranslator, which @ibacher says is the right place to fix this.

The caution from Ian is that the translator already tries to appropriate property, so there’s likely something else going on.