Azure-docs: How to use setProperty or addProperty for child properties?

Created on 16 May 2019  Â·  21Comments  Â·  Source: MicrosoftDocs/azure-docs

I have a json object that I want to set the value of a child property, but the documentation doesn't show how to do it.

For example, say I have the following json in a variable called entity:
{
"id": 1234,
"name": "test",
"metadata": {
"version": 3
}
}

If I do setProperty(variables('entity'), 'name', 'test2'), it updates the name correctly.
If I do setProperty(variables('entity'), 'metadata/version', 4), it created a new property called metadata/version in the root of the object. I also tried just 'version' but it added the property to the root.

How can I update the version in the metadata object?


Document Details

⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Pri1 assigned-to-author logic-appsvc product-question triaged

Most helpful comment

Also, why can't a setProperty() self reference when used in a set variable step? If I want to update a variable containing json using the setProperty function, I have to create another variable to store the return value and then set the first variable to the second, very annoying and just adds cost to the logic app

All 21 comments

@Rosewood99 Thanks for the Comment. We are actively investigating and will get back to you soon.

Also, why can't a setProperty() self reference when used in a set variable step? If I want to update a variable containing json using the setProperty function, I have to create another variable to store the return value and then set the first variable to the second, very annoying and just adds cost to the logic app

@Rosewood99 I have assigned this to content author to review and update as appropriate.

@Rosewood99, thanks for your feedback. I have a question to the product team about this scenario and have opened a doc work item to address.

@Rosewood99, here's the response from the product team for how to access a JSON object's child property with the setProperty() function:

setProperty(variables('entity')['metadata'],'version', 4)

@Rosewood99, regarding your question about the "Set variable" action, can you provide a screenshot to show what you want to do?

CC: @kevinlam1 to help with the response

@ecfan setProperty(variables('entity')['metadata'],'version', 4) will not work, the return value will only be the metadata portion of the object, not the whole entity object.

That is part of the work around where I stored a new variable called metadata the value for setProperty(variables('entity')['metadata'],'version', 4), then in the original entity I had to do setProperty(variables('entity'), 'metadata', variables('metadata')), but that is a two step process. I want a single line to update a child property.

Then that led to the fact that in my entity variable I could not do setProperty(setProperty(variables('entity')['metadata'],'version', 4), 'metadata', variables('metadata')) because it is self referencing the entity variable. @kevinlam1

I tried you scenario and if you have a variable that is initialized with the object described above:
"Initialize_testObject":{ "inputs": { "variables": [ { "name": "testObject", "type": "Object", "value": { "id": 1234, "metadata": { "metadataprop1": "fooz", "version": 3 }, "name": "test" } } ] }, "runAfter": {}, "type": "InitializeVariable" }

Then you can have the following expression:
"@setProperty(variables('testObject'),'metadata', 'setProperty(variables('testObject')['metadata'],'version',5))"

Which returns:
{ "id": 1234, "name": "test", "metadata": { "metadataprop1": "fooz", "version": 5 } }

@ecfan @kevinlam1 Thanks, but that doesn't answer about self referencing a variable. Your solution still requires two variables. At a bare minimum I want to do this:

"Set_variable": {
                "inputs": {
                    "name": "entity",
                    "value": "@setProperty(variables('entity'),'metadata', 'setProperty(variables('entity')['metadata'],'version',5))"
                }

My concern is that so often what should be a simple single step is a convoluted multiple step process driving up the cost of Logic Apps.

Setting properties in json should be as simple as addOrUpdateProperty(jsonObject: variables('myJson'), path: '/multiple/layers/deep', propertyName: 'myProp', value: 'someValue'). Clean, simple, and easy to decipher for the next developer reading the code. Even better would be something like patchJson(originalJsonObject, updateJsonObject) so that multiple properties could be set at once.

The same thought process goes for getting the value of properties when it is in child property especially when the path could be a dynamic variable passed in. It works easily if you can guarantee that the property is in the root such as variables('myJson')[variables('propName')], but the syntax for ['multiple']['levels']['deep'] is not possible with a dynamic path, and since you can't do ['multiple/levels/deep/propName'] I had to come up with this convoluted way of converting to xml so I could use xpath to read a dynamic property value: xpath(xml(json(concat('{"instance":', variables('myJson'), '}'))), concat('/instance', variables('myPath'), '/text()'))[0] Creating the instance property to wrap 'myJson' is only because to convert to xml there can only be a single root element.

I love Logic Apps far better than dealing with Mulesoft, but the one thing I thought they did fairly well with Dataweave was object manipulation whether it's java, xml, or json objects. I know I can create Azure Functions to handle more complex stuff, and I am doing that, but I feel that full featured json manipulation should be built in.

Thank you for sharing your feedback. I used a variable as a means to create the object with, but you could have replaced the initial @variable() reference with a direct reference to the output of the action that you are manipulating so you don't need an intermediary variable.
Regardless, I hear your feedback which is to make it easier to do these types of manipulations in Logic Apps. We recently released a new feature called inline code that allows you to write a view lines of JavaScript directly in the Logic App with the intent to handle scenarios such as these.
https://docs.microsoft.com/en-us/azure/logic-apps/logic-apps-add-run-inline-code

Assign a value to a child properties is one of the most basic features that any tool like this should have.
I´ve the same problem as @Rosewood99.
In future releases, this feature is going to be supported by AZ Logic Apps?

@kevinlam1, The Inline Code feature looks like that doesn't have access to existing variables.
So is not possible to change a child property of an object.

Inline code is in preview and access to variables is coming soon. For now, if you want to use inline code to manipulate the object you can put the object in a Compose instead.

Hi @lommez, can you confirm whether is this issue resolved for now? Thanks!

@lommez & @kevinlam1, are there any other doc action items for this feedback? Otherwise, I'll close out the thread. Thanks!

please-close

Hi @lommez, can you confirm whether is this issue resolved for now? Thanks!

Sorry for the delay @ecfan . But this issue is not resolved for me.
I'm still waiting the inline code to have access to variables

Please reopen

Hi all,

Thank you all for your feedback. The functions help topic has been updated with more info about how to use the addProperty() function and setProperty function for accessing child properties in JSON objects. Also, we updated the help topic for the inline code action to note that variables support isn't available yet, but we'll update that topic when that capability is available. You can post feedback about the help topic itself on that particular page.

Because this GitHub issues forum is for doc feedback, and inline code support for variables is product-related, please share your product feedback at the Logic Apps customer feedback forum instead so that others can also vote.

Thank you again for providing your valuable about the docs!

To set the child property in a child object, use a nested setProperty() call instead.

Can you provide an example of what you mean by using a nested setProperty() call?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bdcoder2 picture bdcoder2  Â·  3Comments

jharbieh picture jharbieh  Â·  3Comments

monteledwards picture monteledwards  Â·  3Comments

behnam89 picture behnam89  Â·  3Comments

Agazoth picture Agazoth  Â·  3Comments