0

Hi I am hoping this is a simple question.

In my pre-request-script I am getting a JSON object back from a GET.

This JSON object has 10 fields. I would like to add 2 more.

I tried myJson.add and myJson.push but those don't work. How would I accomplish this task? I am then taking that myJson and adding it to a push request in the test.

Thanks in Advance

xk0der
  • 3,660
  • 4
  • 26
  • 35
Scott H.
  • 3
  • 4
  • 1
    Provide how your JSON is structured and what code you currently have in your pre-request script, to make it easier for folks to help you out! – xk0der Jul 31 '20 at 04:50

1 Answers1

2

With the lack of data in the description, I'm providing a very general answer

Assuming myJson contains your JSON string, first parse it to convert the JSON data to an object as follows:

let jsonObj = JSON.parse(myJson);

Once done, now you can add/remove/update the data - depending on the structure of your JSON.

For example, assuming your data is an array:

[ 
    {
       "data": "value"
    },
    {
       "data": "value2"
    }
]  

You can add another element by using:

jsonObj.push({"data": "value3"});

Once you are done updating the data, convert it back to string as follows:

myJson = JSON.stringify(jsonObj);

You can now store this in an environment variable etc for use in the Postman request.

Reference: https://learning.postman.com/docs/sending-requests/variables/

xk0der
  • 3,660
  • 4
  • 26
  • 35