Unable to parse data from a variable

Hi,
I am getting the error when I try to read data from external json file:

JSONError: Unexpected token u in JSON at position 0 

I’m using this in the script to reference the data:

let testData = JSON.parse(pm.collectionVariables.get("testData"); 

I have taken this from the information in a different community post.

Please assist to resolve JSONError to read data from jsonfile to validate against multiple sets of response body. Thank you.

Hi @rupanivenky. Welcome to the Postman Community!

Can you console.log(pm.collectionVariables.get(“testData”)) to see if it logs a JSON data?

It’s returning - undefined when I run console.log(pm.collectionVariables.get(“testData”)).

testData.json is the datafile where I have the expected response data which I need to validate against the response body.

I am launching the collection runner and uploading the json file “testData.json” as suggested in previous post.

Please assist. Thank you.

image

I can’t see the rest of the file but that’s not a valid format of a JSON datafile.

It would be like this with a key (which would be the variable name) and then the object:

[
    {
        "testObject": {
            "test_1": 1,
            "test_2": 2,
            "test_3": 3
        }
    }
]

The formatting isn’t the best here but it would result in this:

It would parse it the way that you had it:

Hi @danny-dainton @gbadebo-bello

What is the object name I am supposed to provide there, based on previous thread, I am creating the json file with expected key and values.

Please refer this: CSV comparison to a responseBody - :person_raising_hand: Help - Postman Community

Here is my full JSON data file info:

Here is the testcase to retrieve the response and validate against json file testData:

Here is how, I am trying to get testData.json while Run collection:

Please assist on how to proceed further with the requirement I have to validate set of responses against the JSON file data. Thank you.

Thank you for providing more information on the script and process you have, it’s difficult to know what’s happening when one 1 person can see that :smile:

What are the values of that variable pm.collectionVariables.get(“testData”)? Why are parsing that?

If you’re reading those from an external data file you would need to use pm.iterationData.get("the_var_name")

@danny-dainton Thank you for that. But I need to read the data of all JSON File and loop through to validate against the response body sets as in image below. Please assist.

The code snippet that you copied from the other post is for a different context and the scenario is not the same. That was taking different parts of the datafile and check that against the corresponding parts of the response data. You’re also using the references like element.ID in your scripts which isn’t defined in your datafile because it’s id.

You also removed the code comment which tells you how to use a datafile. In Step 2, it’s using a Collection variable to mimic a datafile, unless you have created a testData variable at the Collection level which contains the same data as your JSON file, it wouldn’t work.

// Step 1 - parse response
const response = pm.response.json();

// Step 2 - define data file. 
let testData = JSON.parse(pm.collectionVariables.get("testData"));
// You would change this to read from the data file.  
// You can access the iteration data from the special "data" variable
// let testData = data

// Step 3 - Iterate over the test data\input file
testData.forEach(element => {

    // Step 4 - Test each record in the input file
    // Using string literals to create a custom test case name
    // So its easier to know which record failed
    pm.test(`response keys match for ID ${element.ID}`, function () {

        // Step 5
        // Search the response using the ID from the testData
        let search = (response.find(obj => { return obj.id === element.ID }));

        // Step 6
        // You don't need to test the ID is correct
        // You can just check that the search is not undefined
        pm.expect(search).to.not.be.undefined;
        // console.log(search);

        // Step 7
        // You can now assert on the other fields
        // Example using bracket notation, just in case of special characters
        pm.expect(search.address.city).to.eql(element["address-city"]);
        // Example using dot notation
        pm.expect(search.workCenterCode).to.eql(element.workCenterCode);
    });
})

If you wanted to check the whole object in the datafile against the whole response object, the structure of the datafile and the script would need to change in many places.

Thank you for that. I need to check the whole object in the datafile against the whole response object, please share the input on how to achieve that. Thank you.

The JSON data files are an array of objects.

Each object will be an iteration that is run through the collection runner.

Each iteration will only have the current iterations data.

It won’t have the whole object\data (which I suspect is what the problem is here).

Therefore, if you want to read in the whole dataset, you will need to change the structure of your JSON data file, so that it has an array with a single object that contains the rest of the data. You would then parse that top level array to a variable to be compared against your response.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.