Removing parameters outputted in JSON response. How to remove array and only show specific array of fields?

Hello - looking for help here. I’m learning so I really appreciate any help the community can provide.

When I make a GET request, the data is returned in JSON format. I specify certain QSP’s, just standard filters and sorting. In the response, there is an array of data for the “sort” and “filters” and also other meta data about the call.

I’m looking for help to remove the “sort”, “filters”, and “additional_info” from the response body. Is there a pre-script request that can be used or another method? As you can see in the screenshot, I still want the “transactions” array of data to be displayed, just not the other meta data about the GET request.

THANK YOU FOR YOUR HELP!

1-28-2020 10-47-25 AM

Hi @Akochiss! Welcome to the community. :slight_smile:

There isn’t a way to remove these properties from the actual response, but you can manipulate the response data in a Test script and then do whatever you need to do with it. Example test script:

// declare a variable, and assign it the response body as a value
let responseData = pm.response.json() 

// delete unwanted properties
delete responseData.sort; 
delete responseData.filters;
delete responseData.additional_info;

// Store your modified response data as a string in the environment for later use
pm.environment.set('data',JSON.stringify(responseData))

Then, say you have another request where you need to POST this data. If it’s stored exactly as it needs to be sent, you can just include it anywhere in your request by adding {{data}}.

…or if you need to reference this in another script, use:

// retrieve stringified object from the environment
let dataString = pm.environment.get('data');
// parse the string back into a JSON object
let dataJson = JSON.parse(dataString);
// then do something with with...

Hope that helps! Let me know if you have any questions.

Thank you @john-paul…
Still hitting a snag that I hope you can help with!

When i add the script to the “Tests” in postman, and then declare the {{data}} in the body, the response doesn’t seem impacted.

Here is where the {{data}} is stored in the body:

And the response still showing the “transactions”, which is expected/desired - but not the “Sort”, “Filters” and “additional_info”.

Any idea on what I may be missing?

Thank you so much

Here is the response - even with the script and {{data}} in the body.

Adding that the content header type is urlencoded

Hi @Akochiss.

The script I shared won’t affect the actual response — what it does is extract data from your response, and whittle it down to just what you need. This subset of the original response data can then be inserted wherever else you need it (like in a subsequent request).

Can you tell me a bit more about what you’re trying to accomplish over all?

Thanks @john-paul.

I’m trying to dump the JSON response into an integration platform (Dell Boomi) where I can then map the fields to a system of record (via a SOAP API POST into batch).

Since the response has the meta data associated with the GET request (the “sort”, “filtering”, and “additional_data”, the integration platform cannot distinguish between valid “transaction” data which should be integrated to the system of record and the additional GET request data which isn’t a transaction but just details about the response.

So the ultimate goal is to only return the result “transactions” array - and not include any other data.

Thanks again

Ah, got it. Thanks for the details.

In that case, you’ll just want to store the array contents instead of omitting everything else.

So, based on the response you posted, this would look like:

var responseJson = pm.response.json();
var transactions = responseJson.result.transactions;
var transactionsStringified = JSON.stringify(transactions);
// check to see if format matches what the API expects
console.log(transactionsStringified)

pm.environment.set('data',transactionsStringified);

This code should work if transactionsStringified matches what your API expects. I’ve included a console.log() so you can check the Postman console (command + option + C on Mac) for the exact structure.

Thanks @john-paul… really helpful.

I’ve dropped the script into Postman, but all the console logs look exactly the same. In the console, the response doesn’t include the query params and other GET call related data - but can you confirm what I should expect with this type of script.

Thanks very much for your patience.

Hi @Akochiss I want to make sure I correctly understand how your requests are setup.

I’m assuming you have:

  1. One GET request that gets the transactions data
  2. One POST request that sends the transactions data — this is where the {{data}} variable should be included

Is that right?

Thanks @john-paul.

I see the gap. Can you please walk though the POST request piece a bit more. This is where I’m mixed up with how to execute the POST. I create the variable but I see now that I am creating the POST request.

Thanks.

Ah, got it.

So, the first GET request should have the script that grabs the response data and sets an environment variable.

The POST request should contain the {{data}} variable, wherever it is needed. My guess is that this would be in the request body (assuming you want to submit the whole transaction array), but I would check the documentation for your API to determine this.

1 Like

Thanks very much for all your help @john-paul — you’re a pro!

1 Like

No problem! Glad I could help. :slight_smile: Let me know if you need anything else!

@john-paul i am looking to similar kind of solution, hope you will help me here.

image

i have 1 POST and 1 PATCH call, POST response body should be the PATCH request body by removing certain properties.

on the above snap, i have mentioned status needs to be removed in passengers properties and some fields in contactInformation needs to be modify and then i should use that data in PATCH call. how to do this ?

Hey @LokeshReddy219

Can you post the raw response body code rather than an image of it please - It makes replicating things on other people machines easier.

Which fields need to be removed? - Can you provide an example of what the response from the POST request is and what the Body for the PUT needs to look like.

below is the response of post request.

{
    "id": "acf1f17e-f9d1-403b-b145-8d0aa58191a9",
    "destinationCountry": {
        "code": "USA",
        "name": "United States"
    },
    "dataCollectEligible": true,
    "allowCheckin": true,
    "passengers": [
        {
            "id": "d5e80205-c9a8-4f56-a8b7-b3a023af53c7",
            "firstNameNumber": "01",
            "lastNameNumber": "01",
            "givenNames": "YUEBAIMS",
            "surname": "LIU",
            "customerId": "9970260871751",
            "contactInformation": {
                "optOutAllowed": false,
                "refused": false,
                "contactInformationRequirements": [
                    {
                        "requirementType": "ADDRESS",
                        "fields": [
                            {
                                "label": "Street Address, Location, or Hotel Name",
                                "dataType": "string",
                                "maxLength": 30,
                                "name": "street",
                                "value": "563 ACDJKNN",
                                "ordinal": 1,
                                "required": true
                            },
                            {
                                "label": "City",
                                "dataType": "string",
                                "maxLength": 30,
                                "name": "city",
                                "value": "CONK",
                                "ordinal": 2,
                                "required": true
                            },
                            {
                                "optionType": "COUNTRY_SUBDIVISION",
                                "label": "State",
                                "dataType": "string",
                                "maxLength": 2,
                                "name": "countrySubDivision",
                                "value": "GA",
                                "ordinal": 3,
                                "required": true
                            },
                            {
                                "label": "ZIP Code",
                                "dataType": "string",
                                "maxLength": 9,
                                "name": "postalCode",
                                "value": "30020",
                                "ordinal": 4,
                                "required": true
                            }
                        ],
                        "noneAllowed": false,
                        "required": true
                    },
                    {
                        "requirementType": "EMAIL",
                        "fields": [
                            {
                                "label": "Email Address",
                                "dataType": "string",
                                "dataFormat": "email",
                                "maxLength": 25,
                                "name": "Email Address",
                                "value": "",
                                "ordinal": 1,
                                "required": true
                            }
                        ],
                        "noneAllowed": true,
                        "required": true,
                        "refused": true
                    },
                    {
                        "requirementType": "PRIMARY_PHONE_NUMBER",
                        "fields": [
                            {
                                "optionType": "COUNTRY",
                                "label": "Country",
                                "dataType": "string",
                                "maxLength": 3,
                                "name": "countryCode",
                                "value": "1",
                                "ordinal": 1,
                                "required": true
                            },
                            {
                                "label": "Primary Phone Number",
                                "dataType": "string",
                                "maxLength": 12,
                                "name": "number",
                                "value": "30939009998",
                                "ordinal": 2,
                                "required": true
                            }
                        ],
                        "noneAllowed": false,
                        "required": true
                    },
                    {
                        "requirementType": "SECONDARY_PHONE_NUMBER",
                        "fields": [
                            {
                                "optionType": "COUNTRY",
                                "label": "Country",
                                "dataType": "string",
                                "maxLength": 3,
                                "name": "countryCode",
                                "value": "",
                                "ordinal": 1,
                                "required": true
                            },
                            {
                                "label": "Secondary Phone Number",
                                "dataType": "string",
                                "maxLength": 12,
                                "name": "number",
                                "value": "",
                                "ordinal": 2,
                                "required": true
                            }
                        ],
                        "noneAllowed": true,
                        "required": true,
                        "refused": true
                    }
                ]
            }
        }
    ],
    "status": "SUCCESS"
}

for the PUT "status": "SUCCESS" needs to be remove and some fields needs to be update in "requirementType": "SECONDARY_PHONE_NUMBER","requirementType": "PRIMARY_PHONE_NUMBER","requirementType": "EMAIL" and "requirementType": "ADDRESS",

What would the request body for the PUT request look like? Saying what things would need to be updated doesn’t really answer the question. :smiley:

  • Where are you getting these details from to update those different values?
  • Are those contained in the POST response body?

I would like to do below operations.

  1. First call will be the retrieve contact post information(we will get passenger info) -POST
  2. Second call one will be the update contacts information(we need to update passengerinfo - PUT

In the above post i have added the PUT request body which is POST request response too.

You can use the delete operator to remove the status from the POST response body before saving this as a variable.

let postResponse = pm.response.json()

delete postResponse.url

pm.environment.set("putRequestBody", JSON.stringify(postResponse))

This is an example using the Postman Echo service - Here I am getting the response body, deleting the url property before saving this as a stringified JSON object.

I could then use this {{putRequestBody}} variable in my PUT request body which contains the data that I’ve modified.

1 Like