0

i have test case to compare against the source kept in Kafka message.

I noticed the structured is not same.

no missing field, but the structure is not arranged in the same sequence.

how do i make the result converted same as the source structure?

code to retrieve the message, then decode the base64 format and prettyprint the result.

def responseList = new JsonSlurper().parseText(consumeMessage.getResponseText())

println('response text: \n' + JsonOutput.prettyPrint(JsonOutput.toJson(responseList)))

def decoded = new JsonSlurper().parseText(new String(responseList[0].value.decodeBase64()))

println('response decoded text: \n' + JsonOutput.prettyPrint(JsonOutput.toJson(decoded)))

below is the result printed at console

2019-11-20 16:36:44.934 DEBUG oingDRToAllocationVerification-DynamicID - 10: decoded = JsonSlurper().parseText(new java.lang.String(responseList[0].value.decodeBase64()))
2019-11-20 16:36:44.945 DEBUG oingDRToAllocationVerification-DynamicID - 11: println("response decoded text: 
" + JsonOutput.prettyPrint(JsonOutput.toJson(decoded)))
response decoded text: 
{
    "contexts": [
        {
            "activityId": "c2884e63-d30d-48a3-965c-0b33202885c2",
            "incomingTimestamp": "2019-11-20T08:36:29.0829958Z",
            "sourceName": "DispenseOrderService",
            "timestamp": "2019-11-20T08:36:29.0829958+00:00",
            "userId": "unknown"
        }
    ],
    "dispenseOrder": [
        {
            "dispenseRequestType": "DISPENSEORDER",
            "id": "6320112019043628",
            "items": [
                {
                    "administrationInstructions": "drug intake information test 123",
                    "dispenseAsWritten": false,
                    "id": "cda92ec7-3191-4b7b-a972-7f4545146db4",
                    "itemId": "Augmentn",
                    "quantity": 100
                },
                {
                    "administrationInstructions": "drug intake information test 234",
                    "dispenseAsWritten": false,
                    "id": "19e00776-b08d-47c8-930b-76ddc01f0ff4",
                    "itemId": "Clopidogrl",
                    "quantity": 200
                },
                {
                    "administrationInstructions": "drug intake information test 456",
                    "dispenseAsWritten": true,
                    "id": "0a5b0f4a-366d-4fa7-a0b8-2e8c83f4af13",
                    "itemId": "Adenosine",
                    "quantity": 300
                }
            ],
            "locationId": "Pharmacy Jewel East",
            "piiIdentifiers": {
                "doctorId": "b502f046-fb1e-4fcf-8135-a7a13cfb47f6",
                "patientId": "fe49b461-8eeb-46d5-b995-a31cdaaa35f3",
                "pharmacistId": "b502f046-fb1e-4fcf-8135-a7a13cfb47f6"
            },
            "priority": 4,
            "state": "NEW",
            "type": "Test ingest type"
        }
    ],
    "messageClass": "DispenseRequestV1",
    "messageId": "83e94dac-dfb6-49d7-8ca0-219d155fecce",
    "notifications": [
        
    ],
    "operation": "Add",
    "timestamp": "2019-11-20T08:36:29.0952632+00:00"
}

below is the source. the result after conversion is not same as source. as in the structure is not arranged accordingly.

{
  "operation" : "Add",
  "dispenseOrder" : [ {
    "id" : "6320112019043628",
    "locationId" : "Pharmacy Jewel East",
    "piiIdentifiers" : {
      "patientId" : "fe49b461-8eeb-46d5-b995-a31cdaaa35f3",
      "doctorId" : "b502f046-fb1e-4fcf-8135-a7a13cfb47f6",
      "pharmacistId" : "b502f046-fb1e-4fcf-8135-a7a13cfb47f6"
    },
    "priority" : 4,
    "state" : "NEW",
    "type" : "Test ingest type",
    "dispenseRequestType" : "DISPENSEORDER",
    "items" : [ {
      "id" : "cda92ec7-3191-4b7b-a972-7f4545146db4",
      "itemId" : "Augmentn",
      "quantity" : 100,
      "dispenseAsWritten" : false,
      "administrationInstructions" : "drug intake information test 123"
    }, {
      "id" : "19e00776-b08d-47c8-930b-76ddc01f0ff4",
      "itemId" : "Clopidogrl",
      "quantity" : 200,
      "dispenseAsWritten" : false,
      "administrationInstructions" : "drug intake information test 234"
    }, {
      "id" : "0a5b0f4a-366d-4fa7-a0b8-2e8c83f4af13",
      "itemId" : "Adenosine",
      "quantity" : 300,
      "dispenseAsWritten" : true,
      "administrationInstructions" : "drug intake information test 456"
    } ]
  } ],
  "messageId" : "83e94dac-dfb6-49d7-8ca0-219d155fecce",
  "timestamp" : "2019-11-20T08:36:29.0952632+00:00",
  "messageClass" : "DispenseRequestV1",
  "contexts" : [ {
    "userId" : "unknown",
    "timestamp" : "2019-11-20T08:36:29.0829958+00:00",
    "activityId" : "c2884e63-d30d-48a3-965c-0b33202885c2",
    "incomingTimestamp" : "2019-11-20T08:36:29.0829958Z",
    "sourceName" : "DispenseOrderService"
  } ],
  "notifications" : [ ]
}
user2201789
  • 1,083
  • 2
  • 20
  • 45

1 Answers1

1

As json.org says:

An object is an unordered set of name/value pairs.

So, different JSON methods/libraries might order them in a different way. You shouldn't rely on order of name/value pairs when working with JSON.

(If order is very important to you, you might try using suggested solution from this post.)

Mate Mrše
  • 7,997
  • 10
  • 40
  • 77