Skip to main content
Announcements
SYSTEM MAINTENANCE: Thurs., Sept. 19, 1 AM ET, Platform will be unavailable for approx. 60 minutes.
cancel
Showing results for 
Search instead for 
Did you mean: 
mr_burns
Contributor
Contributor

How to extract JSON only if tRest provides data in jsonpath

Hi,

I have a tRest and a tExtractJSONFields component like this:

 

0695b00000bFlF6AAK.pngthe property "Loop Jsonpath query" of tExtractJSONFields looks like this:

 

"$.itemgroup[*].item"

 

but sometimes the arrray 'itemgroup' is not sent back from the server, becuase of e.g. an error, then I get this:

 

com.jayway.jsonpath.PathNotFoundException: Missing property in path $[itemgroup]

 

How can I handle this error now? The tExtractJSONFields component has to go on and should not die.

 

Thanks for any help!

Labels (3)
5 Replies
zjing
Contributor III
Contributor III

hello ,

you need share your your file json

and the configuration de compoment tExtractJsonFileds

 

according to errer, I think of a worng configuration in compoment tExtractJsonFileds

mr_burns
Contributor
Contributor
Author

in case of success the json looks like this:

 

{

"itemgroup": [

"item1": {

"name": "item1",

"qty": 5

},

 

"item2": {

"name": "item2",

"qty": 17

}

]

}

 

in case of an error it looks like this:

 

{

    "error": {

        "text": [

            "Something went wrong"

        ]

}

 

The problem is that in case of error the node 'itemgroup' never is returned from the server and this causes the component to die .

I cannot us the tDie component to catch the error because in my job I have a global tLogCatcher with sends an eMail in case of error.

And especially the error of tExtractJsonFields I want to handle in a different way like e.g. put some error message in a database, but only in case of 'itemgroup' is not returned from the server. In any other error cases of tExtractJSONfields the global tLogCatcher should handle this.

 

Thanks for further help...

 

Anonymous
Not applicable

Maybe you can check response string if it contains the 'itemgroup' element, if yes, extract data from response string using tExtractJsonFields, if no, send an email.

eg:

..tRest-main-->tJavaRow--runIf1--tFixedFlowInput--main--tExtractJsonFields

--runIf2--tSendMail

on tJavaRow:

context.response=input_row.Body;

//context.response is a context variable with string type.

if(input_row.Body.contains("itemgroup"){

globalMap.put("continueExtractData",true);

}else{

globalMap.put("continueExtractData",false);

}

 

set the condition of runIf1 as:

(boolean)globalMap.get("continueExtractData")

 

set the condition of runIf1 as:

!(boolean)globalMap.get("continueExtractData")

 

tFixedFlowInput: generate the response string as input row, define one column and set its value as: context.response

 

Regards

Shong

 

mr_burns
Contributor
Contributor
Author

thanks shong,

 

I already could help myself and found an easier way.

Instead using a tRest component I use a tRestClient component now.

tRestClient provides an extra output called 'error' to handle my errors:

 

0695b00000bFzpDAAS.png 

The tJavaFlex (you can use a tRavaRow or tJava component as well) is necessary to create the Body for a Post request executed by tRestClient. There is no "Http Body" property like in tRest.

You need to create following output fields for tJavaFlex:

 

0695b00000bFzq1AAC.pngThe Java code looks like this:

 

String payload = "";

payload += "{";

payload += " \"itemgroup\":\"ABC123\", ";

payload += "\"filter\": [\"CurrYear=2023\"]";

payload += "}";

 

row1.string = payload;

row1.body = payload;

 

may this helps someone with a similar problem.

SallyStory
Contributor
Contributor

I have also facing the same issue.