Highlighted
New Member..
New Member..
1668 views

How to add variable to JSON file for REST API script

Hi,

I am using LR 12.02 and was wondering if anyone knew how to call a variable into a JSON file (request body) that I use as an Extra File in my Web protocol script (here the file is called CheckoutIndivCust.json).

 

        web_custom_request("web_custom_request",
        "URL=https://connect-qa3.paciolan.corp/pdi/v1/cart/{CartID}/checkout?policyCode=EXPERIENCE",
        "Method=POST",
        "TargetFrame=",
        "Resource=0",
        "EncType=application/json", 
        "Mode=HTTP",
        "BodyFilePath=CheckoutIndivCust.json",
        "Referer=",
        "Body=",
        LAST);

 

Here is a snippet of my JSON script:

        "amount": 86.00
    }]
}

 

Instead of hardcoding the value of 86.00 I want to call a variable that I created called 'Balance'

    web_reg_save_param_ex(
        "ParamName=Balance",
        "LB=\"balance\":",
        "RB=,\"",
        "Ordinal=1",
        SEARCH_FILTERS,
        LAST);

 

Tags (1)
0 Likes
2 Replies
Highlighted
New Member..
New Member..

Re: How to add variable to JSON file for REST API script

I am just looking to replace the value "86.00" with a variable that I did a web_reg_save_param_ex for that was name "Balance"

 

 

 

{
    "patron": {
        "account": null,
        "lastName": "Vo",
        "firstName": "Andrew",
        "middleInitial": null,
        "companyName": null,
        "isOrganization": false,
        "address": {
            "type": "P",
            "address1": "5171 Cali",
            "address2": null,
            "address3": null,
            "address4": null,
            "locality": "Irvine",
            "region": "CA",
            "postalCode": "92617",
            "countryCode": "US"
        },
        "phoneNumber": {
            "type": "H",
            "number": "7143831741",
            "formattedNumber": "+1 (714)383-1741",
            "extension": null,
            "countryCode": "US"
        },
        "emailAddress": {
            "type": null,
            "address": "Test@pac.com"
        }
    },
    "comments": "Andrew is awesome",
    "tags":["Andrew",
    "is",
    "Cool"],
    "userDefinedFields": [{
        "index": 1,
        "value": "FOO"
    },
    {
        "index": 2,
        "value": "BAZ"
    },
    {
        "index": 3,
        "value": "ARE"
    },
    {
        "index": 4,
        "value": "BRZ"
    },
    {
        "index": 5,
        "value": "FRS"
    },
    {
        "index": 6,
        "value": "TSA"
    },
    {
        "index": 7,
        "value": "AAA"
    },
    {
        "index": 8,
        "value": "BBB"
    }],
    "delivery": {
        "method": "W4",
        "emailAddress": "andrew_vo@comcastspectacor.com",
        "id": null
    },
    "payments": [{
        "paymentMethod": "C",
        "cardholderName": null,
        "accountNumber": null,
        "accountPIN": null,
        "documentNumber": null,
        "expirationMonth": null,
        "expirationYear": null,
        "cardVerificationNumber": null,
        "billingAddress": null,
        "billingPostalCode": null,
        "billingCountryCode": null,
        "routingNumber": null,
        "bankAccountNumber": null,
        "amount": 86.00
    }]
}

0 Likes
Highlighted
Frequent Contributor.
Frequent Contributor.

Re: How to add variable to JSON file for REST API script

Hi,

You could use C file functions (fopen, fread) to read JSON file into char array. Then use C string functions to replace the substring. In LR12.53 we have more convenience way to manipulate JSON. But in LR12.02 you may have to treat it as string.

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.