Another newbie ques: using REST API to make multiple changes in a list

hello,

I am VERY new to REST API. I have an issue where I have to create a list, (already done through the GET command). From this list, (disk array volumes), I need to identify specific volume numbers, and from those “hits”, modify (using PUT), the volume label.

1 Like

Hi @rvstout

Can you share what you have so far?
Mainly the response/list from your GET?

1 Like

Hello,

I would love to, but I would need to make some modifications, plus I will not be able to send the entire content since this is a govt agency…

Would that be acceptable? I mean, to get the jist of what I have and what I am dealing with”

Thanks for the response…

1 Like

If you can obfuscate the data that would be fine.

It was basically to see what structure the response is in, as to work out the necessary dot notation for getting values.

1 Like

Thanks!

I have attached a snippet of the list…

I want to be able to locate the vol by “ldevId” (lines 2 and 30), and from there, modify the “label” (lines 27 and 55).

Any advice you can give me will be greatly appreciated

Thanks again!

(Attachment array vol list snippet.txt is missing)

1 Like

Maybe try a screenshot?

1 Like

Unfortunately, postman rejected the .txt… I have sent a .png of the list, however you won’t see the last few lines, but

They are repetitive from the first vol set…

1 Like

And to clarify, when you say;

I want to be able to locate the vol by “ldevId” (lines 2 and 30), and from there, modify the “label” (lines 27 and 55).

Is the modification submitted with a POST or PUT?

1 Like

The modification would be with a PUT

1 Like

You could search your JSON response by using something like this;

//Parse the JSON response
const jsonData = pm.response.json();
//Find the first element in the provided array that have ldevId 140
let result = jsonData.find(a => a.ldevId === 140)
//Set label as a collection level variable
pm.collectionVariables.set("label", result.label);
//Output label to console
console.log(result.label);

However, I’m not 100% sure how you intend on using/modifying this ‘Label’ value?
(Are you ‘PUT-ing’ just the label value or re-sending the entire JSON response back to the server?

If you are simply adding a string to the label, you could do something like this;

//Set label as a collection level variable and add _XYZ to the end
pm.collectionVariables.set("label", result.label + "_XYZ");
//Output label to console
console.log(pm.collectionVariables.get("label"));

Once this has been saved into the collection variables, you could call it inside your PUT.

Hopefully that makes sense / helps.

1 Like

If you need the entire JSON response (the object retrieved by ‘find’), then try this;

//Parse the JSON response
const jsonData = pm.response.json();
//Find the first element in the provided array that have ldevId 140
let result = jsonData.find(a => a.ldevId === 140)
//Show original values
console.log(result);
//Change the label to something else
result.label = "My new label";
//Output the modified JSON response 
console.log(result);

Example output;

1 Like