0
votes

JSON body for an HTTP request needs to look like:

{
  "OrderId":"234",
  "SupplierId":"JJ889",
  "OrderedProducts": [
    {
      "ProductId":"123",
      "Sku":"ABC123",
      "Description":"Thing 1"
    },
    {
      "ProductId":"435",
      "Sku":"XYZ987",
      "Description":"Thing 2"
    }
  ]
}

And I have a CSV file that looks like:

ProductId,Sku,Description
123,ABC123,Thing 1
435,XYZ987,Thing 2
....

But when I substitute "ProductId":"${ProductId}" (and the other variables for sku and description) in the HTTP Request body data I end up with:

{
  "OrderId":"234",
  "SupplierId":"JJ889",
  "OrderedProducts": [
    {
      "ProductId":"123",
      "Sku":"ABC123",
      "Description":"Thing 1"
    },
    {
      "ProductId":"123",
      "Sku":"ABC123",
      "Description":"Thing 1"
    }
  ]
}

How do I ensure my collection of products is unique (i.e. different CSV line) per request?

2
Are there always 2 products per request? - RaGe
No, not always 2 products. I'd love to be able to have a variable number of products per request. I suppose I could structure the CSV to have 4 or 5 products per line and have multiple requests each with up to 5 products and just sort of fudge it that way. Still would like a more elegant solution - DevonS
In which case, as far as I can tell, you'll have to use a beanshell preprocessor to custom construct your JSON before using it in a HTTP request. - RaGe

2 Answers

0
votes

If you have always 2 products per request, just organize your CSV this way:

ProductId1,Sku1,Description1,ProductId2,Sku2,Description2

And use the new variable names.

0
votes

Use combination of __StringFromFile() and __javaScript() functions like:

{
  "OrderId":"234",
  "SupplierId":"JJ889",
  "OrderedProducts": [
    {
      "ProductId":"${__javaScript("${__StringFromFile(file.csv,,,)}".split("\,")[0],)}",
      "Sku":"${__javaScript("${__StringFromFile(file.csv,,,)}".split("\,")[1],)}",
      "Description":"${__javaScript("${__StringFromFile(file.csv,,,)}".split("\,")[2],)}"
    },
    {
      "ProductId":"${__javaScript("${__StringFromFile(file.csv,,,)}".split("\,")[0],)}",
      "Sku":"${__javaScript("${__StringFromFile(file.csv,,,)}".split("\,")[1],)}",
      "Description":"${__javaScript("${__StringFromFile(file.csv,,,)}".split("\,")[2],)}"
    }
  ]
}
  • __StringFromFile() function reads next line from file.csv each time it's being called
  • __javaScript() function allows executing arbitrary JavaScript code, in above case split() function to break the line into ProductId, Sku and Description.

See Functions and Variables User Manual chapter and How to Use JMeter Functions posts series for more information on JMeter functions.