Add Json stored in a variable to a request body

Hi all!

I been trying to find a way to store a json in a variable in a GET request, store it in a collection variable and use it in a POST request in a collection run.
So far, I been able to store it with no issues in the GET request, but having issues to post it correctly.

I tried using:
“{{MyJsonVar}}”, [{{MyJsonVar}}], ["{{MyJsonVar}}"], {{MyJsonVar}} and still no luck. Is there anything I’m missing? Any help would be greatly appreciated!

Cheers!

Hey @NRS, could you share more details on the exact structure of your requests and json value? Feel free to redact sensitive information :smile:

Yes of course! So the structure goes like this:
image

The GET request have this in the tests:

try{
    // parse Json response object
    let result = JSON.parse(responseBody);
    
    let existingJobId = result.id;
    
    let customFields = result.properties;
    console.log('Original Job bag custom fields: ' + customFields);
    let stringifiedCustomFields = JSON.stringify(customFields);
    console.log('stringified custom fields: ' + stringifiedCustomFields);
    
    // make sure we clean up any previous values.
    pm.variables.unset("properties");
    
    tests["Job Bag Exists"] = existingJobId !== null;
    tests["Has custom fields"] = customFields !== null;
    
    pm.variables.set("job_id", existingJobId);
    pm.variables.set("properties", stringifiedCustomFields);

    }catch(e){
        console.log('Error parsing JSON: ' + e);
    }
    
    postman.setNextRequest('DuplicateJobBag');

Then in the POST body, I have this:

{
	"CopyFromId": {{job_id}},
	"Name": "{{next_job_name}}",
	"CompletionDate": "02/02/2011",
	"Properties": "{{properties}}"
}

properties is a variable in the collection and is the JSON i need to send in the duplicate request.
Sadly, is sending that as null.

Cheers!

@kunagpal I must say I’m a bit surprised what I see.
Have you marked @NRS 's description of a problem as a solution to the problem? :smiley:

1 Like

@oskarkaminski.dev you’re right, @kunagpal where is the solution ?

I have the same issue. I need to passe an json object as variable.
My pre-request Scripts:

var billingAddress = {
    "address": "('è`@&) 217 rue du loup billing",
    "address2": "complement billing ('è`@&)",
    "city": "XXX ('è`@&)",
    "country": "FR",
    "firstName": "XXXX billing ('è`@&)",
    "lastName": "XXXX billing ('è`@&)",
    "zipCode": "76000",
    "company": "XXXX Inc billing ('è`@&)"
};

//pm.environment.set('billingAddress', JSON.stringify(billingAddress));
pm.variables.set('billingAddress', JSON.stringify(billingAddress));

My body request:

{
    "orderId": {{$timestamp}},
    "notified_url":"https://webhook.site/XXX-4ff8-a04d-XXX",
    "billingAddress": "{{billingAddress}}",
   ...
}

I tried several syntaxes for get billingAddress but look as follow in my request:

{
“orderId”: 1642410931,
“notified_url”:“https://webhook.site/XXX-4ff8-a04d-XXX”,
“billingAddress”: “{{billingAddress}}”,

}