# Unable to parse data from a variable

**URL:** <https://community.postman.com/t/unable-to-parse-data-from-a-variable/59028>\
**Category:** Help Hub\
**Tags:** tests, variables, scripts, jsondatafile\
**Created:** [March 20, 2024, 6:16am UTC](https://community.postman.com/t/unable-to-parse-data-from-a-variable/59028 "2024-03-20T06:16:23Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![rupanivenky](https://avatars.discourse-cdn.com/v4/letter/r/ecae2f/32.png) [@rupanivenky](https://community.postman.com/u/rupanivenky)\
**Post date:** [March 20, 2024, 6:16am UTC](https://community.postman.com/t/unable-to-parse-data-from-a-variable/59028/1 "2024-03-20T06:16:23Z")

</div>

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

```auto
JSONError: Unexpected token u in JSON at position 0 

```

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

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

```

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

> [@CSV comparison to a responseBody](https://community.postman.com/t/csv-comparison-to-a-responsebody/54330/6):
>
> Ok, this will be better structured as JSON. The following is an example of an array with two objects. Each line of your CSV will be a separate object and you would include all of the relevant fields you want to check. I’ve only included three fields for testing purposes, but these would match your CSV headers. [ { "ID": 121, "address-city": "LAS", "workCenterCode": "DAY" }, { "ID": 100, "address-city": "AU", "workCenterCode": "DAY" …

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

---

<div class="post-metadata">

**Author:** ![gbadebo-bello](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/gbadebo-bello/32/12175_2.png) [@gbadebo-bello](https://community.postman.com/u/gbadebo-bello)\
**Post date:** [March 20, 2024, 9:18am UTC](https://community.postman.com/t/unable-to-parse-data-from-a-variable/59028/2 "2024-03-20T09:18:21Z")

</div>

Hi @rupanivenky. Welcome to the Postman Community!

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

---

<div class="post-metadata">

**Author:** ![rupanivenky](https://avatars.discourse-cdn.com/v4/letter/r/ecae2f/32.png) [@rupanivenky](https://community.postman.com/u/rupanivenky)\
**Post date:** [March 20, 2024, 1:37pm UTC](https://community.postman.com/t/unable-to-parse-data-from-a-variable/59028/3 "2024-03-20T13:37:47Z")

</div>

> [@gbadebo-bello](#):
>
> console.log(pm.collectionVariables.get(“testData”))

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](https://us1.discourse-cdn.com/getpostman/original/3X/0/a/0a3da2323af659feedfa0df00e1e0c9cbc798eea.png)

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [March 20, 2024, 4:30pm UTC](https://community.postman.com/t/unable-to-parse-data-from-a-variable/59028/4 "2024-03-20T16:30:27Z")

</div>

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:

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

```

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

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/7/5/75c1bbde20705615927fb1cae8c831a35671ad6c.png)

It would parse it the way that you had it:

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/3/8/38dd449c87b356d75dcf503b0ce2fd05f390ac38.png)

---

<div class="post-metadata">

**Author:** ![rupanivenky](https://avatars.discourse-cdn.com/v4/letter/r/ecae2f/32.png) [@rupanivenky](https://community.postman.com/u/rupanivenky)\
**Post date:** [March 20, 2024, 5:05pm UTC](https://community.postman.com/t/unable-to-parse-data-from-a-variable/59028/5 "2024-03-20T17:05:27Z")

</div>

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 - 🙋 Help - Postman Community](https://community.postman.com/t/csv-comparison-to-a-responsebody/54330/5)

Here is my full JSON data file info:

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/8/f/8f6013e7e628ebdb5be3e1364546169e1e11b643.png)

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

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/b/3/b3d6ea7b31cc33341d9c17a35775be5f90bd213d.png)

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

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/a/2/a2c26a861a7a3e10e07b7b9d47027977c6d1e06e.png)

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

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [March 20, 2024, 5:13pm UTC](https://community.postman.com/t/unable-to-parse-data-from-a-variable/59028/6 "2024-03-20T17:13:07Z")

</div>

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 😄

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")`

> **[Manage API data and workflows using Postman JavaScript objects | Postman...](https://learning.postman.com/docs/writing-scripts/script-references/postman-sandbox-api-reference/#using-data-variables-in-scripts)**
>
> Manage API data and workflows using Postman JavaScript objects: documentation for Postman, the collaboration platform for API development. Create better APIs—faster.

---

<div class="post-metadata">

**Author:** ![rupanivenky](https://avatars.discourse-cdn.com/v4/letter/r/ecae2f/32.png) [@rupanivenky](https://community.postman.com/u/rupanivenky)\
**Post date:** [March 20, 2024, 5:23pm UTC](https://community.postman.com/t/unable-to-parse-data-from-a-variable/59028/7 "2024-03-20T17:23:41Z")

</div>

@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.

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/5/6/56061295770cd4a8097ed93ae4885abdef4be1a5.png)

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [March 20, 2024, 8:28pm UTC](https://community.postman.com/t/unable-to-parse-data-from-a-variable/59028/8 "2024-03-20T20:28:22Z")

</div>

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.

```auto
// 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.

---

<div class="post-metadata">

**Author:** ![rupanivenky](https://avatars.discourse-cdn.com/v4/letter/r/ecae2f/32.png) [@rupanivenky](https://community.postman.com/u/rupanivenky)\
**Post date:** [March 21, 2024, 2:15pm UTC](https://community.postman.com/t/unable-to-parse-data-from-a-variable/59028/9 "2024-03-21T14:15:17Z")

</div>

> [@danny-dainton](#):
>
> wanted to check the whole object in the datafile against the whole response object,

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.

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [March 25, 2024, 11:41am UTC](https://community.postman.com/t/unable-to-parse-data-from-a-variable/59028/10 "2024-03-25T11:41:00Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/system/32/29796_2.png) [@system](https://community.postman.com/u/system)\
**Post date:** [April 24, 2024, 11:41am UTC](https://community.postman.com/t/unable-to-parse-data-from-a-variable/59028/11 "2024-04-24T11:41:10Z")

</div>

Closed on April 24, 2024, 11:41am UTC

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