Highlighted
Trusted Contributor.. Trusted Contributor..
Trusted Contributor..
139 views

How to call a function within a JSON request to dynamically replace a value?

Hi Everyone,

I need some advise if the below is doable.

I have a json request which has large number of dynamic values. I am thinking of building a function which can be called to dynamically replace all required values. Is there any way I can call a function from within the JSON request body? Due to lot of dynamic values, I don't want to correlate each field separately, save to a parameter and then use it in the json body.

Example: In below case to replace the OrderHeaderKey value 20190510154317231367741, I wan to achieve something like:

Body={\"scWizardDefId\":\"isccs.order.wizards.orderSummary.OrderSummaryWizard\",\"isWizard\":true,\"scControllerInput\":{\"Order\":{\"EnterpriseCode\":\"Test-RETAIL\",\"DocumentType\":\"0001\",\"OrderHeaderKey\":myfunction(keyword),\"OrderNo\":\"Y100101181\",\"DraftOrderFlag\":\"N\",\"PriceInfo\":{\"Currency\":\"CAD\",\"TotalAmount\":\"0.00\"},\"PersonInfoShipTo\":{\"Company\":\"XXX Store\",\"PersonInfoKey\":\"20190510145003231363534\",\"FirstName\":\"\",\"ZipCode\":\"T0J0Y0\",\"Title\":\"\",\"City\":\"XXX\",\"EMailID\":\"\",\"MiddleName\":\"\",\"DayPhone\":\"\",\"MobilePhone\":\"\",\"AddressLine2\":\"555 HIGHWAY EAST\",\"AddressLine1\":\"BOX 6000\",\"Department\":\"GROCERY\"}}}&scCSRFToken={CSRF}&scFlag=Y"

char* myfunction(char* keyword)
{
logic....
return dynamicvalue
}

 

Thanks

 

0 Likes
2 Replies
Highlighted
Acclaimed Contributor.. Acclaimed Contributor..
Acclaimed Contributor..

Re: How to call a function within a JSON request to dynamically replace a value?

You are in this way implementing LR-parameter substitution yourself.This is something that I never considered in  my 20 years of working with LR...

I see from your example that you are using C-code, so you might do something with sprintf() and replace each function call in your example with %s and place the function call as arguments to the end of sprintf() function. Watch the order of %s and function calls arguments. Modern sprintf() functions can do smart things by numbering/name the arguments. Not sure what LR supports today.

Success,

Signature:
Reward community members who take time to respond and help.
0 Likes
Highlighted
Trusted Contributor.. Trusted Contributor..
Trusted Contributor..

Re: How to call a function within a JSON request to dynamically replace a value?

Thanks.
Even I never did this before. The application code is so weird that it is passing like 20+ dynamic values in the request when they could have passed the unique key only. I guess I have to correlate each field and use the parameter in the JSON body.
0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.