Forum Discussion

abhinaykumar15's avatar
abhinaykumar15
Occasional Contributor
2 months ago

Unable to parse multiple json received in kafka Eventhub

Hi All,

I have a Eventhub to receive messages as and when they are published.I may receive multiple json messages.So i parse each of them using the Property-list-data field and try to assert certain fields in

all the received jsons.

But i am getting a error when i try to access a particular field in the received json.

I am trying something like this.

 
import groovy.json.*
import groovy.util.*
import groovy.json.JsonSlurper
 
 
//Parse JSON content in the request
def data = context.expand( '${Receive Message#data#$[*]}' )
def json=new JsonSlurper().parseText(data)
 
assert json.eventID=='1234546' || '1234545'-----This works fine
 
def i=1
while(i!=0)
{
 
def data2 = context.expand( '${Receive Message#data}' )
def json2=new JsonSlurper().parseText( data2)
log.info json2[i]
if (json6[i].eventID=='1234546')--------------This gives Error -Error is mentioned at the bottom of this post.
{
i=0;
log.info "Test Success"
}else{
i=i+1
}
 
}

 

Error :

groovy.lang.MissingPropertyException: No such property: eventID for class: java.lang.String
error at line: 20

 

Json that is received is as .I receive multiple such json in the same format.

{
   "eventID" : "1234546",
   "eventTimeStamp" : "2017-07-21T17:32:28Z",
   "eventType" : "EVENT_PUB",
   "Identifier1" : "",
   "Identifier2" : "",
   "shipinfo" : {
      "InfoNumber" : "123456",
      "originLoc" : "IND",
      "destLoc" : "IND",
     
   },
  
  
}

Please help .What am i missing here.

  • Humashankar's avatar
    Humashankar
    Champion Level 2

    Hi abhinaykumar15 

    The MissingPropertyException you're encountering suggests that the script is attempting to access a non-existent property within the parsed JSON object.

    To resolve this issue, consider the following suggestions.

    First, verify that the JSON structure is consistent and includes the eventID property in all JSON objects. Moreover, ensure correct indexing when accessing json2[i], confirming that the index i is within the array's bounds and that json2[i] is a JSON object rather than a string.

    Hope this helps - Happy to help further!!
    Thank you very much and have a great one!
    Warm regards