i have json file with this data
[
{
“rolename”: “Number one”,
“roledescription”: “Number one”,
“rolepermission”: [“manage_users”,“view_user_logs”],
“roletype”: “client”
}
]
i want to extract data from “rolepermission” and put it in body request
this api accept data like this
{
Can you share the code that you are currently using to extract & populate the rolepermission variable? This should be a straightforward amendment, but it will be easiest to help you if we are able to see your current implementation.
I’m not sure how you are currently setting the rolepermission variable. Is this being set in the Get permissions request?
The code below is an example, which you should hopefully be able to adapt.
Suppose you currently have code such as this:
var myRolePermissions = pm.response.json().role.permissions;
pm.collectionVariables.set("rolepermission", myRolePermissions);
This causes the list to be flattened to a single string (in my case, [“one”,“two”] has become “one,two”):
What you need to do is explicitly retain the JSON formatting when storing to variable. So, you would use JSON.stringify() when saving the variable, like this:
var myRolePermissions = pm.response.json().role.permissions;
pm.collectionVariables.set("rolepermission", JSON.stringify(myRolePermissions));
Now you will see that the variable has retained its list formatting:
Note that you will need to modify the body of your Create Role request, to remove the hardcoded square brackets in the permissions section, because these will now be loaded from your variable. In other words:
Yeah I Get The Role Permission Variables From My Get Reqeust
And I Copy That In My Json File
But In My Request “permissions” Accept Arraye
Are You Sure I Can Do It ?
i Dont Want To Store In Envirment Variable
i want to read from json file
OK, yes you will need to convert the data back from string to array format. Here is what I recommend:
Go to Pre-request Script tab of the Create Role request, and insert the following code:
// Get rolepermission data (currently stored as string)
var roles = pm.iterationData.get("rolepermission");
// Convert roles back to a JSON array
pm.globals.set("rolepermission_array", JSON.stringify(roles));
Then you should modify the Create Role body to use this new array variable:
I have a similar problem but I need to get the value of a variable that is inside another array of a json file that I am trying to upload through Runner.
This is an extract from the file:
[
{
“title”: “NI :: U :: Derivar a B2C Soporte Móvil”,
“active”: true,
“actions”: [
{
“field”: “current_tags”,
“value”: [
“derivacion_soporte_b2c_movil”
]
}
],
and this would be my part of my json body
{
“trigger”: {
“title”: “{{title}}”,
“active”: {{active}},
“actions”: [
{
“field”: “{{a_field}}”,
“value”: “{{a_value}}”
}
],
With your code I tried to put something like this together, but it doesn’t read the data from the json file.
// Get rolepermission data (currently stored as string)
var a_field = pm.iterationData.get(“actions.field”);
console.log(a_field);
var a_value = pm.iterationData.get(“actions.value”);