Unlock a world of possibilities! Login now and discover the exclusive benefits awaiting you.
Hi all
I have a data set and need to crapped it into json request before POST to an API
The json request contains many nested json hierachy, so that I cannot use the tRESTClient
I try to use the tREST but I don't know how to input the data into it from a tMap (this component join my data and ready to build the json input) and neither know how to extract Json from it (there is not "string" field as in tRestClient)
Could you please guide me for this?
Thanks
@phancongphuoc ,you should populate in HTTP body of tRest component,check the below link will help you more.
https://community.talend.com/t5/Design-and-Development/POST-JSON-with-REST-API/td-p/150057
hi Manodwhb
Thanks for your help, I use the tREST with Http Body string to build up the Json structure
But the json string is really complicated and not easy to maintain
Do we have any other approach to simplify this stage?
I saw that there is a tWriteJsonField, but I don't know how to leverage it into my case.
---------- the jSon String is like this-----------------
"{\"version\":1"
+",\"request_id\":\""+ ((String)globalMap.get("AltanaInput.request_id")) + "\""
+",\"date_time\":\""+ ((String)globalMap.get("AltanaInput.date_time"))
+"\",\"items\":[{"
+"\"item_id\":\""+((String)globalMap.get("AltanaInput.item_id"))+"\""
+",\"date_time\":\"2019-11-21T12:23:59\""
+",\"declared\":{"
+"\"hs_code\":\""+ ((String)globalMap.get("AltanaInput.declare_hs_code")) +"\""
+",\"description\":\"" + ((String)globalMap.get("AltanaInput.declare_description")) + "\""
+",\"value\":\"" + ((String)globalMap.get("AltanaInput.declare_value")) + "\""
+",\"unit\":\"" + ((String)globalMap.get("AltanaInput.declare_unit")) + "\""
+"}"
+",\"route\":{"
+"\"origin_country\":\""+((String)globalMap.get("AltanaInput.route_origin_country"))+"\""
+",\"lading_country\":\""+ ((String)globalMap.get("AltanaInput.route_lading_country"))+ "\""
+",\"unlading_country\":\""+ ((String)globalMap.get("AltanaInput.route_unlading_country"))+ "\""
+",\"lading_port\":\""+ ((String)globalMap.get("AltanaInput.route_lading_port")) + "\""
+",\"unlading_port\":\""+ ((String)globalMap.get("AltanaInput.route_unlading_port")) + "\""
+"}"
+",\"foreign_party\":{"
+"\"id\":\""+ ((String)globalMap.get("AltanaInput.foreign_party_id"))+ "\""
+",\"name\":\""+ ((String)globalMap.get("AltanaInput.foreign_party_name"))+"\""
+",\"address\":\""+ ((String)globalMap.get("AltanaInput.foreign_party_address")) + "\""
+",\"country\":\""+ ((String)globalMap.get("AltanaInput.foreign_party_country")) + "\""
+"}"
+",\"local_party\":{"
+"\"id\":\""+ ((String)globalMap.get("AltanaInput.local_party_id"))+"\""
+",\"name\":\""+ ((String)globalMap.get("AltanaInput.local_party_name")) + "\""
+",\"address\":\""+ ((String)globalMap.get("AltanaInput.local_party_address"))+"\""
+",\"country\":\""+ ((String)globalMap.get("AltanaInput.local_party_country"))+ "\""
+"}"
+",\"quantity\":{\"value\": 3000,\"unit\":\"box\"},\"weight\":{\"value\":0.25,\"unit\":\"kg\"},\"volume\":{\"value\":0.5,\"unit\":\"cubic in\"}}]}"
Hi @phancongphuoc ,
I had a case similar to yours with a huge json to construct and it was boring.
Nothing but escaping all the quotation marks was painful ahah
As i see there are some parameters that you pass through global map and other that are fixed.
So you can maybe try to develop a routine who creates your java object with the jsonsimple class.
When you call the method, pass the global map fields as parameters.
In my case, it helped a lot because i was calling the method several times in my job don't know if it will be as useful for you but just sharing.
Regards,
Martin
Hi Martin,
Sorry for long delay.
I finally use a tJava and set the json construction into a concept string variable, and then pass the variable into the Http Body
It should be a little bit easier to maintain for me.
Hi,
I need to pass arrays through context variables in trest component.Could you please suggest how it can be done