cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
forstera
Helper III
Helper III

Problem of rebuilding an object after separating it to modify a part of it

Hello all,

I need to modify a JSON object before sending it back to a server (REST API).

My object looks like this : 

forstera_0-1633609063532.png

so I first separated my object to get the part I had to modify (the 'SearchFields' part) (Thanks to  @DamoBird365 for the help)

each element of my array looks like this : 

forstera_2-1633609266455.png

So I made an array filter function to retrieve the element I had to modifiy than I made the modification using the following function ; 

forstera_1-1633609160228.png

 

it seems to work fine but now, but now I have to rebuild my original object (with the modified part) before I can send it to my server and there I have a lot of problems.

 

I created an object variable and now I'm trying ro rebuild the object i get the following error : Variable « ModifiedObjectModifie » of type « Object » can't be initialised or updated with a value of type 'Array'.

I can understand that but how can I transfer my modified array into an object ? 

of is there another way to proceed ? 

Thanks to all for your help

 

1 ACCEPTED SOLUTION

Accepted Solutions
ekarim2020
Community Champion
Community Champion

Here is an option to create (build) a new JSON object containing the changed properties. It changes SearchText to ### in the Apply to Each loop. It should give you some ideas and may be come up with something better.

ekarim2020_1-1633709288610.png

ekarim2020_2-1633709350115.png

ekarim2020_3-1633709376666.png

ekarim2020_4-1633709407507.png

 

ekarim2020_5-1633709469832.png

varNewJSON now contains:

 

 

{
  "Id": 1434,
  "DataClassId": "Anlass",
  "Designation": "PA-ListClasses"
}

 

 

 Initialise an array that will store the SearchFields for the newJSON object:

ekarim2020_6-1633709619178.png

Compose SearchFields contains:

ekarim2020_9-1633709981403.png

Loop through each element of the SearchFields Array. In this loop you could use a condition to make changes to any property. In the example below I am changing all SearchText to ####:

ekarim2020_7-1633709780149.png

varSearchFields array now contains:

ekarim2020_11-1633710233873.png

Add SearchFields to the newJSON object:

ekarim2020_14-1633710390464.png

Compose Add Property SearchFieldArray to varNewJSON contains the full, updated JSON object:

ekarim2020_15-1633710494979.png

Optional step as Compose Add Property SearchFieldArray to varNewJSON probably gives us what we need. varNewJSON is written out to:

ekarim2020_16-1633710549725.png

ekarim2020_17-1633710651259.png

 

 

 

 

View solution in original post

6 REPLIES 6
ekarim2020
Community Champion
Community Champion

I'm not sure if this is what you are looking for, but when you set the value of property in the compose action, you need to write it out (save it back) to the variable:

ekarim2020_0-1633640715022.png

 

ekarim2020_1-1633640888555.png

Ellis

Dear @ekarim2020 , thanks very much for your help, I'm going to make a test but my first impression is not very good because ... I understand what you did but - maybe I'm wrong  - in my case, I need first to filter an array or make a loop to find the element I need to modify. Then, I apply the setProperty function which makes a new object.. the problem is that this new objet is just a part of the original one and not the full one. In your example (correct me if i'm wrong) you put the entire object into a variable than apply the setproperty than save it back to the varialbe... isnt' it ?

Anyway, I'm going to make some tests with your solution and keep you informed 🙂

Thanks again very much and have a nice day 🙂

So, I'm trying to work with your solution but if I understand well, I need to work with my entire object if I want to avoid the creation of a new one. So here, as you can see here above with that partial view of my object, it contains an array of values. So I can't filter it first because if I do thant, it's going to create a new object with only the filtered part of my object.  As I need first to know which row of my array I need to modify ... 

forstera_0-1633675846345.png

For example, If I need to modify the second record of my array, could I do something like :

setProperty(item?['SearchFields']?['Id'] = 2,'SearchText','MyFilterValue') ?
 
Thanks very much 🙂

  

Unfortunately, this didn't work

So I created a new variable with the full content of my object. As you can see, my object contains an element of type array ..

Then, I applied the SetProperty function to an element of my object (item #2 of my array) but the output is not the whole modified object but only the element #2 of my array 😞

 

forstera_2-1633683704595.png

 

 

I'm afraid that the only option is making a loop with all elements of my obect and re-create a new objet inside it by adding every element of my loop. What do you mean ? 

 

ekarim2020
Community Champion
Community Champion

Here is an option to create (build) a new JSON object containing the changed properties. It changes SearchText to ### in the Apply to Each loop. It should give you some ideas and may be come up with something better.

ekarim2020_1-1633709288610.png

ekarim2020_2-1633709350115.png

ekarim2020_3-1633709376666.png

ekarim2020_4-1633709407507.png

 

ekarim2020_5-1633709469832.png

varNewJSON now contains:

 

 

{
  "Id": 1434,
  "DataClassId": "Anlass",
  "Designation": "PA-ListClasses"
}

 

 

 Initialise an array that will store the SearchFields for the newJSON object:

ekarim2020_6-1633709619178.png

Compose SearchFields contains:

ekarim2020_9-1633709981403.png

Loop through each element of the SearchFields Array. In this loop you could use a condition to make changes to any property. In the example below I am changing all SearchText to ####:

ekarim2020_7-1633709780149.png

varSearchFields array now contains:

ekarim2020_11-1633710233873.png

Add SearchFields to the newJSON object:

ekarim2020_14-1633710390464.png

Compose Add Property SearchFieldArray to varNewJSON contains the full, updated JSON object:

ekarim2020_15-1633710494979.png

Optional step as Compose Add Property SearchFieldArray to varNewJSON probably gives us what we need. varNewJSON is written out to:

ekarim2020_16-1633710549725.png

ekarim2020_17-1633710651259.png

 

 

 

 

View solution in original post

Wow ! Thanks very much @ekarim2020  ; it's sound great ! I'm going to make a test these next days and keep you informed ! Thanks again and have a nice week-end

Helpful resources

Announcements
UG GA Amplification 768x460.png

Launching new user group features

Learn how to create your own user groups today!

Community Connections 768x460.jpg

Community & How To Videos

Check out the new Power Platform Community Connections gallery!

M365 768x460.jpg

Microsoft 365 Collaboration Conference | December 7–9, 2021

Join us, in-person, December 7–9 in Las Vegas, for the largest gathering of the Microsoft community in the world.

Top Solution Authors
Top Kudoed Authors
Users online (1,351)