Need help on how to upload json file with array
Getting 500 server error
with error message " “data”: “Property ‘readings’ has value that is not of type ArrayNode (but com.fasterxml.jackson.databind.node.ObjectNode)”"
You need to include sample code, including screenshots of the request, detailing which bits you need to come from the array, and an example of the array\JSON.
Thanks for replying @michaelderekjones PFB
Pre-request script : - pm.variables.set(requestBody
, JSON.stringify(pm.iterationData.toObject()))
Body : - {
}
{
“deviceId”: “8076334e-81d”,
“readings”: {{requestBody}}
}
Variables:-
JSON File :
Plaese have a look.
I would recommend using https://jsonformatter.org/ or similar to minify the JSON, and then store it in a CSV file. Give the CSV file a header called “json” or “readings”.
When you run this through the Collection Runner, it technically has one record, so will run the request once.
In the body, remove the readings element (as we will add it in the pre-request script).
{
"deviceId": "8076334e-81d"
}
In your pre-request script, you import the string using the data tag and update the body at the same time.
var readings = JSON.parse(data.json);
const body = JSON.parse(pm.request.body.raw);
body.readings = readings;
pm.request.body.raw = body;
@michaelderekjones do you want me to put
var readings = JSON.parse(data.json);
const body = JSON.parse(pm.request.body.raw);
body.readings = readings;
pm.request.body.raw = body;
in pre-request as getting error
Remove the “readings” element from the body (and the comma at the end of the deviceId).
The pre-request script will add the readings element back in using the data from the CSV file.
You are running this through the collection runner?
Using a CSV file with a single column with a header called “json” with your json array included?
You can also add the following to the end of the pre-request script to check what the updated body looks like.
console.log(pm.request.body.raw);
Hey @michaelderekjones by any chance can we connect on meet to solve this ?
Create a CSV file with a header.
The following is an example, which just contains the tag and value.
Create a folder and add your POST request.
My example is using Postman Echo.
The body just needs to include the deviceID element.
The pre-request script does the rest of the magic.
It retrieves the data from the CSV file in the first line.
Then it gets the current request body.
Then adds the readings element using the test data.
Before updating the body which is what the request will actually use.
You run it through the Collection runner pointing it to the csv file.
This is the results of the console log, which shows the body that was used,
Thanks alot @michaelderekjones ! It works This time data get POST successfully
Hi @michaelderekjones I want to use timestamp from one response as request for other API through CSV.
Can you please guide me through how can I directly store timestamp of API response in csv file and use as an request through runner. There are total 96 timestamp in a response body.
PFB
You don’t.
You get the response from your first call and then create an array of all of the timestamps using the JavaScript map function.
Once you have the array, you have two options.
You can have a loop in the Tests tab of the first request that uses sendRequest() to hit the new API with the details from the array.
Or you can create a second request and use the array.shift() method to loop though the array.
An couple examples of using array shift is here.
https://community.postman.com/t/loop-through-values-from-get-to-next-put-request/41899/11
These are complex scenarios that you are trying to accomplish, and where I don’t mind giving to advice, you need to try these things out yourself and if you have issues, you can then ask more specific questions.
@michaelderekjones can you please tell how to use JavaScript map function for above example.
It’s a JavaScript function.
Please Google it first, and if you run into issues, show the code you’ve tried.
W3Schools is a good place to start as any.
Hint: Your reading element is an array, so this is what you need to target with the map.
The map function only works against arrays. If the element is not an array, it will error.
I have successfuly segregate timestamp from API response and store in variable, can you guide me trhrough next step
If you want to use the array.shift() method, then you need to store the new array as a collection or environment variable.
When storing arrays, please use JSON.stringify when storing, and JSON.parse when retrieving, otherwise it will just treat it as a string.
If you are going with the sendRequest() option, then you can work directly with the new array.
First step is to try out the array.
newValue.forEach(timestamp => {
console.log(timestamp);
});
Once you have this working, you can then replace the console.log with the sendRequest().
An example using Postman Echo.
// ForEach loop with a sendRequest and tests.
array.forEach(timestamp => {
pm.sendRequest({
url: 'https://postman-echo.com/get?timestamp=' + timestamp,
method: 'GET',
}, function (err, res) {
if (err) {
console.log(err);
} else {
// define the expectedResponse because we can also use this to format the test name.
var expectedResult = timestamp;
// define the actual result, which will be the username from the response
var actualResult = res.json().args.timestamp;
// Our actual test
pm.test(`timestamp = ${expectedResult}`, () => {
pm.expect(actualResult).eql(expectedResult);
});
}
});
});
Read up on the sendRequest() function for more information on how to customise the request. For example how to define a json body to send.
On a side note, there is a slightly easier way to create the array of timestamps.
const response = pm.response.json();
let timestamps = response.readings.map(obj => obj.timestamp);
console.log(timestamps);