How to parse a local json/xml file in a Postman?

Hello,
I want to read lines from my local xml/json file in Pre-request script to add its value for my variables.
For example:
I have a file called OPC.xml, wanna attribute this file to a variable and find lines which have ā€œnameā€ in itself. E.g: ā€œ<Name)Qwerty (/Name>ā€ I used ) because right formula makes it
invisible. Qwerty and other variables should be stored in one array.
How can I do this?
Thanks for any help.

HI @mkolomanski,

Iā€™m afraid Postman canā€™t easily access local files directly because it runs in a sandbox :frowning:. However, there are a few workarounds as follows

  1. Postman can access data through APIā€™s, so if you have the skills to create a local API (eg using Node.js & Express) then you can simply fetch the file using a request, store the data in a global, and use it in subsequent requests. (This isnā€™t as hard as you might think, I ran a workshop with complete novices to create a simple API - see slides 7-14)
  2. You can create a tool that formats your data into a csv or json file suitable to use with the collection runner, or Newman.
  3. You could write some code to inject your data into an environment or globals file prior to running it with Newman.

Youā€™re probably going to have to write some code to get the data from your file into something that Postman can access, but that said, Iā€™ve had to do this in the past and found it easier than I thought it might be (and I am not a dev by any estimation :cowboy_hat_face:)

Let us know which one you think might suit you best, and if you get stuck we can help you.

2 Likes

Hello, I am very grateful for your complex response. But I already have dealt with it in other way.
Although parasing local files will be very useful. I am looking forward to have this option in Postman :slight_smile: Have a nice day!

1 Like

Hello mkolomanski,

Great that if you share how you managed to do it in other way. We have a similar problem.

Best Regards

I just wanted to share a workaround to this problem.

Storing local JSON or XML files is nice for separating and organising your test data. Since there is no native way to do that, if youā€™re willing to compromise slightly and keep your test data stored in the body of a Postman request, this solution might work for you.

If feeding Postman a JSON file for a collection run is not an option (like it was not for me), you can instead store the JSON inside a POST request that sends raw JSON to the Postman echo API:

  1. Set up a POST request that sends data to https://postman-echo.com/post
  2. Define the raw JSON body, maybe something like this:
{
    "user": {
        "email": "yourtestemail@domain.io",
        "password": "password",
        "username": "UserName",
        "first_name": "Grayed",
        "last_name": "Fox",
        "new_password": "!orAng3",
        "new_username": "{{$randomUserName}}",
        "new_first_name": "{{$randomFirstName}}",
        "new_last_name": "{{$randomLastName}}"
    }
}
  1. inside the pre-request script, clear any variables and do any setup you need to, mine looks like:
// Before sending the request we clear all collection variables
pm.collectionVariables.clear();
  1. Set the JSON data in the test script as an environment variable like so:
const jsonData = pm.response.json().data;

pm.collectionVariables.set("collectionData", jsonData);

Now from inside any request you can access the data using the moustache/double curly brackets technique, i.e. you could send a different request with the following JSON body:

{
  "email": "{{email}}",
  "password": "{{password}}",
  "first_name": "{{first_name}}",
  "last_name": "{{last_name}}"
}

To access the data from within a test, use destructuring assignment and take what you need:

const { email, first_name, last_name } =
    pm.collectionVariables.toObject();

Itā€™s not reading a local file, but you can easily duplicate the request to the Postman echo service and basically use that endpoint as a sort of ā€œsetupā€ request that clears old collection values and sets new ones, so you still get the advantage of splitting up test data into organized parts.

Hi! Thanks for the info and detailed explanation. I am needing to do something like this, but slightly different. We have a test case where we want to hit a ā€œsettingsā€ endpoint that will retrieve app settings from the appā€™s json configuration file (duhā€¦), and we need to validate that the endpoint is in fact responding with the proper data (maybe itā€™s kind of a silly testcase, but anywayā€¦ )ā€¦
We are trying to see if there is a way to pickup that json file from postman (itā€™s in the filesystemā€™s app path) in order to compare it with the responseā€¦
Would this approach that you shared be applicable to that scenario?

Thanks!

EDIT: Sorry, stupid questionā€¦ I didnā€™t even take time to read what you were doing in your workaroundā€¦

Hi! I know this is an old post, but I found myself in a scenario where I need to pickup a config file from the filesystem and then hit and endpoint and compare the endpoint response with the actual file read by the endpoint.
I guess that the way to go would be your #3 workaround, which if I understand correctly, would imply creating a script that will allow newman to load the json file to an environment variable in order to have its content available from the testsā€¦ right?
Do you have any examples on how to achieve that?
Thanks!

@jprealini setting your collection environment variable(s) before sending the request would do what you need. You donā€™t actually read a JSON file from within Postman or Newman, you just store the JSON data inside a dedicated utility request which you fire off first.

  1. set the JSON data you are sending to your server as a Postman variable, i.e. pm.collectionVariables.set("myDataEnv", jsonData)

  2. send the JSON data to the endpoint you wish to test

  3. in the tests section of your request, compare the results returned by the endpoint with the environment variable(s) you set, here is an example using destructuring: const { email, first_name, last_name } = pm.collectionVariables.toObject();

You can refer to my earlier answer for a more detailed explanation that basically walks you through the same thing :wink: