Json Response has data with Nested Array Columns but most of the Array columns are empty .So need to identify Records which are not null or have more than one column data

Your question may already have an answer on the community forum. Please search for related topics, and then read through the guidelines before creating a new topic.

Here’s an outline with best practices for making your inquiry.

My question:
patientProcedures,practiceProcedures,visits are nested arrays with columns like
{
id:123
,type:‘some string’
,url :‘Some http Url’
}
But in most of my Json response they are empty .I want to filter and fetch only records which have
not null array values (patientProcedures,practiceProcedures,visits) so that i can test loading them in a database

Details (like screenshots):

I have attached Json with two records for example but my json has 25k records

{
“statusCode”: 200,
“data”: [
{
“type”: “AppointmentV1”,
“lastModified”: “2016-07-08T16:53:13.657Z”,
“duration”: 60,
“statusId”: 5,
“patientProcedures”: ,
“practiceProcedures”: ,
“visits”: ,
“id”: “7000002983226”,
“title”: “Appointment”,
“start”: “2002-08-30T14:30:00.000Z”,
“created”: “2016-04-20T05:27:41.840Z”,
“end”: “2002-08-30T15:30:00.000Z”,
“needsFollowUp”: false,
“needsPremedicate”: false,
“status”: “COMPLETED”,
“note”: “aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa”,
“other”: “CCAdult”,
“bookedOnline”: false,
“patient”: {
“id”: “7000001065834”,
“type”: “PatientV1”,
“url”: “TestUrl”
},
“location”: {
“id”: “7000000000260”,
“type”: “LocationV1”,
“url”: “Url1”
},
“provider”: {
“id”: “7000000000774”,
“type”: “ProviderV1”,
“url”: “Url2”
},
“operatory”: {
“id”: “7000000000550”,
“type”: “OperatoryV1”,
“url”: “Url3”
}
},
{
“type”: “AppointmentV1”,
“lastModified”: “2016-07-08T16:53:13.657Z”,
“duration”: 60,
“statusId”: 5,
“patientProcedures”: ,
“practiceProcedures”: ,
“visits”: ,
“id”: “7000002983227”,
“title”: “Appointment”,
“start”: “2002-08-02T17:30:00.000Z”,
“created”: “2016-04-20T05:27:41.840Z”,
“end”: “2002-08-02T18:30:00.000Z”,
“needsFollowUp”: false,
“needsPremedicate”: false,
“status”: “COMPLETED”,
“note”: “aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa”,
“other”: “CCAdult”,
“bookedOnline”: false,
“patient”: {
“id”: “7000001066365”,
“type”: “PatientV1”,
“url”: “Url4”
},
“location”: {
“id”: “7000000000260”,
“type”: “LocationV1”,
“url”: “Url5”
},
“provider”: {
“id”: “7000000000774”,
“type”: “ProviderV1”,
“url”: “Url6”
},
“operatory”: {
“id”: “7000000000550”,
“type”: “OperatoryV1”,
“url”: “Url7”
}
}
],
“meta”: {
“pagination”: {
“limit”: 2,
“offset”: 0,
“total”: 23895
}
}
}

How I found the problem:

I’ve already tried:

Please use the preformatted text option in the editor when pasting code or JSON.

It’s stops everything being aligned to the left and preserves the quotes so it can be cut and pasted properly.

Something like the following should work.

It uses the JavaScript filter function and checks the length of each array to return the results.

const response = pm.response.json();

let search = response.data.filter(function (obj) {
        return obj.patientProcedures.length > 0 && obj.practiceProcedures.length > 0 && obj.visits.length > 0
});

console.log(search);

pm.test(`search results are not empty`, () => {
    pm.expect(search).to.be.an("array").that.is.not.empty;    
});

Thanks so much for the reply .Could you please help with this as well .I have Json response where “preferredTimes” is List of string object (string array).when there is data field “preferredTimes” exists in json else it will be absent from json.

I want to filter Json where field “preferredTimes” exists and have not empty data .

{
“data”: [
{
“type”: “PatientV1”,
“dateOfBirth”: “1983-01-01”,
“contactMethod”: “TEXT ME”,
“languageType”: “ENGLISH”,
“firstVisitDate”: “2017-08-30”,
“lastModified”: “2018-12-06T16:55:43.710Z”,
“address1”: “94 Essex Street”,
“city”: “Derby”,
“state”: “KS”,
“postalCode”: “67037”,
“preferredTimes”: [
“LATE_MORNING”
]
}
]
}

Can I ask again that you use the preformatted text option when pasting code or JSON examples.

Otherwise it all gets aligned to the left which is hard to read.

// parse directly to the "data" array element.
const response = pm.response.json().data;

console.log(response);
// response will now be an array, so you need to include the index [0]

console.log(response[0].preferredTimes);

pm.test(`preferred time is not empty`, () => {
    pm.expect(response[0].preferredTimes).to.be.an("array").that.is.not.empty;    
});

console.log(response[0].preferredTimes[0]);

pm.test(`preferred time = LATE_MORNING`, () => {
    // preferredTimes is an array with a single element in it
    // so you also need to include the index if you want to target it
    pm.expect(response[0].preferredTimes[0]).to.eql("LATE_MORNING")
});

Iam very sorry for the Json format .As suggested i used online Json formater before pasting the code here but that didn’t helped the format .

In the example, for the sake of simplicity ,I have provided only single object of Data List.There can be any number of elements in Json o/p Data list.

{
“data”: [
{
“id”: “001”,
“type”: “PatientV1”,
“dateOfBirth”: “1983-01-01”,
“contactMethod”: “TEXT ME”,
“languageType”: “ENGLISH”,
“firstVisitDate”: “2017-08-30”,
“lastModified”: “2018-12-06T16:55:43.710Z”,
“address1”: “94 Essex Street”,
“city”: “Derby”,
“state”: “KS”,
“postalCode”: “67037”,
“preferredTimes”: [
“LATE_MORNING”,“LATE_EVENINGS”
]
},
{
“id”: “002”,
“type”: “PatientV1”,
“dateOfBirth”: “1986-01-01”,
“contactMethod”: “Call ME”,
“languageType”: “ENGLISH”,
“firstVisitDate”: “2017-08-30”,
“lastModified”: “2018-12-06T16:55:43.710Z”,
“address1”: “94 Essex Street”,
“city”: “Derby”,
“state”: “KS”,
“postalCode”: “67037”
}  
and so on ...........................
]
}

In id: 002 ,there is no element of preferredTimes and i want to ignore records like id(002) and only need to fetch where preferredTimes exists and not empty .

I’m not 100% sure what you are trying to do here.

You can use the JavaScript filter function against the data array to return a new array of records that fit your criteria.

It’s similar to the previous filter, but you need to check for the property first and then the length otherwise it will throw an error when it hits a record without the property included.

For example


let newArray = response.filter(function (obj) {
    return obj.hasOwnProperty("preferredTimes") && obj.preferredTimes.length > 0
});

console.log(newArray);

But what do you want to do with this afterwards? Once you have this array, what tests do you want to run against the result? As that will define whether you need to loop through the array or not.