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"]
}
Nirmal Dholakiya
  • 181
  • 1
  • 1
  • 6
  • 1
    I had a slightly related issue, of trying to pass a array from a pre request script via pm.variables.set. I solved it by doing `JSON.stringify` on the array, seems to work! – Karthik T Nov 13 '20 at 17:08
  • Does this answer your question? [Postman: Can i save JSON objects to environment variable so as to chain it for another request?](https://stackoverflow.com/questions/41479494/postman-can-i-save-json-objects-to-environment-variable-so-as-to-chain-it-for-a) – Henke Feb 19 '21 at 13:55

6 Answers6

10

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

enter image description here

mpalencia
  • 5,481
  • 4
  • 45
  • 59
6

using variable with a same name will give you an array

enter image description here

WasiF
  • 26,101
  • 16
  • 120
  • 128
  • it' doesn't seems to work, it complains: "variable will be overwritten by a duplicate key"... – Mike D3ViD Tyson Jun 25 '21 at 11:56
  • 1
    @MikeD3ViDTyson It worked, may be they have updated the structure to create an array. If you find the way please let us know. – WasiF Jun 25 '21 at 12:21
  • 1
    The new way of storing is values separated by commas without brackets or quotes - value1,value2,value3 – marhyno Nov 29 '21 at 12:59
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.

MickJaggerr
  • 123
  • 1
  • 2
  • 8
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);
Basim Hennawi
  • 2,651
  • 3
  • 19
  • 31
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.

Nag-Raj
  • 169
  • 1
  • 4
  • 13
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 .