17

Is there the possibility to use an array variable inside postman?

e.g. inside the body of a request:

{
 "myData" : {{arrayVariable}}
}

and inside the data file:

{
 "arrayVariable": ["1", "2", "3"]
}
asked Nov 11, 2016 at 16:15
2

6 Answers 6

11

It's possible, you can even add your own keys

enter image description here

answered Jun 20, 2020 at 10:36

Comments

6

using variable with a same name will give you an array

enter image description here

answered Nov 8, 2018 at 12:05

3 Comments

it' doesn't seems to work, it complains: "variable will be overwritten by a duplicate key"...
@MikeD3ViDTyson It worked, may be they have updated the structure to create an array. If you find the way please let us know.
The new way of storing is values separated by commas without brackets or quotes - value1,value2,value3
5

You can create a JSON body like this:

{
 "myData" : [
 {{arrayVariable}}
 ]
}

And the variable like this:

arrayVariable: "1", "2", "3"

where arrayVariable is the key and "1", "2", "3" is the value.

answered Apr 10, 2018 at 17:28

Comments

3

Postman environment variables are meant to just save data as string, so here you are the workaround to pass array as environment variable/data file to Postman as a string like this:

{
 "arrayVariable": '["1", "2", "3"]'
}

Then, add the following piece of code to parse this variable in pre-request script in Postman like this:

var x = JSON.parse(postman.getEnvironmentVariable("arrayVariable"));
postman.setEnvironmentVariable("arrayVariable", x);
answered Nov 13, 2016 at 17:30

Comments

2

Please create your body request like below

{
 "myData" : ["{{arrayVariable}}"]
}

and there is no change required for data file.you can use as it is.

 {
 "arrayVariable": ["1", "2", "3"]
 }

It will work definatly.

answered Jan 8, 2018 at 10:46

Comments

2

The only solution worked for me was something like the answer MickJagger provided, but I think it needs some clarifications. The JSON data file should be something like this:

[
 {
 "anArray": "1, \"2\", 3.0, \"Foo\", false"
 }
]

which it's value is a string, escaping the quotations for string elements. (Note that this example differs from example provided by original question, to cover more use cases.)

The variables is as MickJagger said:

{
 "value": [{{anArray}}]
}

Maybe other solutions works on previous postman versions, but this solution is tested on postman's latest version (by the time of publishing this answer), i.e. v7.34.0 .

answered Oct 10, 2020 at 17:48

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.