I want to extract array from json file

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
{

"role": {

    "name": "Test",

    "description": "Test",

    "permissions": [

        "manage_users",

        "manage_role",

        "managing_custom_page"

    ],

    "userType":"admin"

}

}
and i convert it to this to extract data from my json file
{

"role": {

    "name": "{{rolename}}",

    "description": "{{roledescription}}",

    "permissions": [

        "{{rolepermission}}"

    ],

    "userType": "{{roletype}}"

}

}

but he send request like this
Vali Migire
“permissions”: [
“manage_users,view_user_logs”
],

instead it should send it like this
“permissions”: [
“manage_users”,
“view_user_logs”
],

what should i do

Hi @science-architect-96,

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 should send request as arraye like this
but i want to extract from json file

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”):

image

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:

image

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:

"permissions": {{rolepermission}}


science-architect-96

6m

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

How are you currently reading from JSON file? Are you using the Runner?

this is how i add this to my collection

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:

{
  "role": {
    "name": "{{rolename}}",
    "description": "{{roledescription}}",
    "permissions": {{rolepermission_array}},
    "userType": "{{roletype}}"
  }
}

Now you should find that the structure of your permissions is correct :slightly_smiling_face:

1 Like

It Fixed
I Dont Know What Should i say
love you