Need help parsing this JSON array

I have an API call that will return a JSON body similar to this:

{
    "__type": "Aldis.Models.v4.Info.CameraStatusModel, Aldis.Models",
    "ActiveCamera": [
        {
            "RectilinearIR": {
                "CalibrationMask": {
                    "Length": 0,
                    "Width": 0,
                    "Id": "3fc388275b724e82819cf44603db69c3",
                    "Polygon": []
                },
                "CameraMasks": {
                    "OcclusionMasks": [],
                    "RoadMasks": [],
                    "ZoneMasks": []
                },
                "CompassHeading": 0,
                "ExtrinsicCalibration": {
                    "HeightFeet": 16
                },
                "IntrinsicCalibration": {
                    "FocalLengthX": 428,
                    "FocalLengthY": 480,
                    "ImageCenterX": 0.5,
                    "ImageCenterY": 0.5,
                    "ImageHeight": 480,
                    "ImageWidth": 720,
                    "RadialDistortion1": 0,
                    "RadialDistortion2": 0,
                    "TangentialDistortion1": 0,
                    "TangentialDistortion2": 0,
                    "Channels": 3,
                    "Depth": 8,
                    "Height": 480,
                    "MaxJpegSize": 140000,
                    "Width": 720
                },
                "IsConfigured": true,
                "MACAddress": "AC:CC:8E:23:35:86",
                "Make": "AXIS",
                "Name": "SW FLIR"
            }
        },
        {
            "Fisheye": {
                "CameraMasks": {
                    "OcclusionMasks": [],
                    "RoadMasks": [],
                    "ZoneMasks": []
                },
                "CompassHeading": 0,
                "ExtrinsicCalibration": {
                    "HeightFeet": 16
                },
                "IntrinsicCalibration": {
                    "Diameter": 0.85625,
                    "ImageCenterX": 0.5109375,
                    "ImageCenterY": 0.50625,
                    "ImageHeight": 960,
                    "ImageWidth": 1280,
                    "ViewAngleDegrees": 185,
                    "Channels": 3,
                    "Depth": 8,
                    "Height": 960,
                    "MaxJpegSize": 140000,
                    "Width": 1280
                },
                "IsConfigured": true,
                "MACAddress": "00:30:53:1d:2c:22",
                "Make": "Basler",
                "Name": "SW Bell"
            }
        }
    ],
    "MissingCameras": [],
    "UnknownCameras": []
}

I want to write a test that checks that the details for each camera. As you can see from the return body, if the camera is a RectilinearIR, the IntrinsicCalibration details are different from those for the Fisheye camera.

As I go through each object in the ActiveCamera array, how do I determine if I have a RectilinearIR camera or if I have a Fisheye?

Have you tried json schema validation?

You can do it like this:

var JSON_SCHEMA = { YOUR_JSON_SCHEMA_HERE }

pm.test("Response use a valid schema", function() {
  pm.expect(tv4.validate(pm.response.json(), JSON_SCHEMA, false, true)).to.be.true;
});

To get json schema for your json data you could use: https://www.liquid-technologies.com/online-json-to-schema-converter

Just paste your json data and generate the json schema to use in test.

No, I haven’t. Thank you for the suggestion. I’ll give it a shot.

One of the developers I work with also suggested I can use something like this:

for (var i in jsonData.ActiveCamera) {
    if ( "RectilinearIR in jsonData.ActiveCamera) {
        // do tests for RLIR
    {
    if ("Fisheye" in jsonData.ActiveCamera) {
        // do tests for FE
    {
{

Do you see any problems with my tests being performed in such a way? Javascript is still pretty new to me, so I’m learning as I go. :slight_smile:

Thanks for the reply.

@paul_gs

or …

var jsData = JSON.parse(responseBody);
var cameraType;

if ( jsData.ActiveCamera.hasOwnProperty("RectilinearIR") ) {
    cameraType = jsData.ActiveCamera.RectilinearIR;
    console.log( cameraType.IntrinsicCalibration.FocalLengthX );
    // do tests for IR
    if ( cameraType.IntrinsicCalibration.FocalLengthX !== 428 ) {
        // do something
    }
}
if ( jsData.ActiveCamera.hasOwnProperty("Fisheye") ) {
    cameraType = jsData.ActiveCamera.Fisheye;
    console.log( cameraType.IntrinsicCalibration.ImageHeigh );
    // do tests for FE 
    if ( cameraType.IntrinsicCalibration.ImageHeight !== 960 ) {
        // do something
    }
}

I only use the schema validation to do just that … validate that the correct JSON structure/schema/variables were returned in the response … not make a decision to run some specific piece of code to actually test the value(s) in the JSON variables.

This site is great for JavaScript references …

https://www.w3schools.com/js/default.asp

No problem. This way you can check every property and even store values for other purpose.

I suggest json schema validation cause it provides a simple way to verify if the json in response have every property it should have and if the value of every property suits specific rules (type, regex, enum validation…). I suggest you to look at json-schema.org maybe it helps to improve your tests.

Hey :wave:

Since it seems like a lot of people are wondering the same thing and end up on this topic, I’ve created a public collection demonstrating the two solutions given above that you can access here:

https://www.postman.com/postman/workspace/postman-answers/collection/9215231-b8d5325e-c9ad-45dd-99f4-88d5c075016a?ctx=documentation

Anyone interested can fork it to their own Postman account and see how it works! :smiley: