I want to automate my API Testing but i am not able to extract value from my json

I Want to automate my API testing but i am not able to read value from json as i have json with multiple array. sample is as below

{
   "ConfigurationParam":{
      "AgentCode":"asdasdsadas",
      "ProductCode":"AS"
   },
   "PlanType":"Individual",
   "InsuredDetail":[
      {
         "InsurerName":"Me",
         "InsuredAge":32,
         "InsurerAgeType":"Year"
      },
      {
         "InsurerName":"Spouse",
         "InsuredAge":30,
         "InsurerAgeType":"Year"
      }
   ],
   "AdditionalDetails":{
      "AvailLoyaltyDiscount":0,
      "HealthPolicyNo":"",
      "PinCode":23131312,
      "MobileNo":"243243242342",
      "DateOfBirth":"1988-12-20T00:00:00"
   }
}

Need help for same

@morianki Welcome to the community :slight_smile:

So we understand that you are trying to parse the response which has the nested arrays.

Can parse using iterations like for loop and can get the element.

Based on your need we can script it. So what’s your plan with this response? What kind of tests you are planning to? :slight_smile:

Manually you can try accessing them using

    var resp = pm.response.json();

    console.log(resp.InsuredDetail[0]);

       console.log(resp.InsuredDetail[1]);

You should have similar sort of queries answered in the forum already. If not Please provide some more insights to us.

1 Like

@bpricilla Thanks for Quick help!

Basically the json i have posted is request json not response json. i don’t have idea how to set nested arrays in csv file ?

@morianki, would you mind being a little more specific on what it is you’re trying to do and what is happening?

Are you having an issue adding data to a request? Is there an issue parsing the response?

1 Like

@allen.helton @bpricilla Let me brief you i want to achieve,

I want to implement End to End automation testing for my API’s, till now i am able read parent tags data from json, i am not able to read nested arrays or not able to set it into csv file, Actually i don’t idea how to set values in csv referring json. I need help to set request json to Csv file for Automation testing.

i am attaching request json below which i need to convert it into csv file.

{
   "ConfigurationParam":{
      "AgentCode":"asdasdsadas",
      "ProductCode":"AS"
   },
   "PlanType":"Individual",
   "InsuredDetail":[
      {
         "InsurerName":"Me",
         "InsuredAge":32,
         "InsurerAgeType":"Year"
      },
      {
         "InsurerName":"Spouse",
         "InsuredAge":30,
         "InsurerAgeType":"Year"
      }
   ],
   "AdditionalDetails":{
      "AvailLoyaltyDiscount":0,
      "HealthPolicyNo":"",
      "PinCode":23131312,
      "MobileNo":"243243242342",
      "DateOfBirth":"1988-12-20T00:00:00"
   }
}

This is csv data till now i have achieved. i feel it is not proper approach so i need help for same.

If I’m reading this correctly, you just need help substituting variables in your request?

If that’s the case, then you can use variables directly in the request body. Make the variable names the header from your csv.

{
  "ConfigurationParam: {
    "AgentCode": "{{AgentCode}}",
    "ProductCode": "{{ProductCode}}"
  },
  "PlanType": "{{PlanType}}",
  ... // Do this for all variables
}

For the columns that are already JSON, i’m not positive, but you might just be able to use it like a normal variable in the request. If not, you would have to stringify the value in the pre-request script.

Pre-request script

const insuredDetail = pm.iterationData.get('InsuredDetail');
pm.variables.set('insuredDetailString', JSON.stringify(insuredDetail));

Then use the variable you set there in the body of the request

{
  ...
  "InsuredDetail": {{insuredDetailString}},
  ...
}