cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
MrRK
Level: Powered On

Json to Array Issue

Hi, really struggling with a flow processing engineer jobsheets.

 

It receives a HTTP request from their PDA's, processes the Json and adds it into two sharepoint list consisting of general information, and products used.

 

Everything in the general list gets added fine, but the products list is a little more troublesome.

 

  • If there are no products listed, then the flow works perfectly.
  • If there are two products, then the flow works perfectly.
  • If there is one product, then the flow fails, with the following error:

 

Unable to process template language expressions for action 'Condition_2' at line '1' and column '2352': 'The template language function 'length' expects its parameter to be an array or a string. The provided value is of type 'Object'. Please see https://aka.ms/logicexpressions#length for usage details.'.

 

 

If there is nothing in the array, then it will return Empty as 'true' in a condition, and exit the  flow. If there are two products listed, then it processes fine in the 'Apply to Each' part of the flow importing the products.

 

I believe the issue I'm having is in the way that the Json is submitted from the PDA manufacturer.

Example of Two products in the Raw Json submitted via the HTTP request.

      "Product1": [
        {
          "Qty": "3",
          "Barcode": null,
          "Item": "Eggs",
          "Price": "3.50"
        },
        {
          "Qty": "4",
          "Barcode": null,
          "Item": "Boxes",
          "Price": "66.00"
        }
      ],

however when there is just one product, the below is the raw json:

      "Product1": {
          "Qty": "4",
          "Barcode": null,
          "Item": "Boxes",
          "Price": "66.00"
        },

I believe my issues are relating to the fact that when there is one product, it's considered a string, rather than an array by the flow.

 

I'm at the end of my knowledge and would really appreciate some help processing the data what ever number of products are listed.

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Community Support Team
Community Support Team

Re: Json to Array Issue

Hi @MrRK,

 

Could you please show a bit more about the two SharePoint lists that you mentioned?

Further, could you please share a screenshot of your flow's configuration?

 

The error message told that the length() function expects its parameter to be an array or a string, but you provide an Object wihtin it. The length() function could not return the number of elements in an Object. More details about the length() function, please check the following article:

https://docs.microsoft.com/en-us/azure/logic-apps/logic-apps-workflow-definition-language#functions

 

I assume that you could get the corresponding content of the Product1 property within Ram json in your flow. If you want your flow to work well when there is one product, you should check if the Raw json is array or object firstly within your flow. If the Raw json is an object, you should convert the Object into an array at first.

 

I have made a test on my side and please take a try with the following workaround:

  • Add a proper trigger, here I use Flow Button trigger.
  • Add a Compose action, Inputs set to Raw json dynamic content from the HTTP request your side. On my side, Inputs set to following Raw json for testing:
{
  "Qty": "3",
  "Barcode": null,
  "Item": "Eggs",
  "Price": "3.50"
}
  • Add a Variables-> "Initialize variable" action, Name set to FinalArray, Type set to Array and Value set to empty.
  • Add a Variables-> "Initialize variable 2" action, Name set to InitializeArray, Type set to Array and Value set to output of "Compose" action.
  • Add a Parallel branch action, within left branch, add a "Set variable 2" action, Name choose FinalArray, Value set to output of "Compose" action. Within right branch, add a Scope action, within Scope action, add a "Compose 2" action, Inputs set to following formula:
array(outputs('Compose'))

Add a "Set variable" action, Name choose FinalArray variableValue set to output of "Compose 2" action. Then click "Menu" button of Scope action as below:14.JPG

Then click "Configure run after", within "Scope should run after" panel, select has failed option and click "Done".

 

  • Under the "Parallel branch" action, add a "Compose 3" action, Inputs set to following formula:
length(variables('FinalArray'))

Then click "Menu" button of Compose 3 action and then click "Configure run after", Within "Compose 3" action should run after panel, we firstly select Set variable 2 action tab, then select is successfull and is skipped option, click "Done" button:16.JPG

Then we select Scope action tab, select is successful option:17.JPG

 

 

Image reference:18.JPG

 

19.JPG

The flow works successfully as below:20.JPG

 

 

Within following "Apply to each" action, Input parameter should set to FinalArray variable, the screenshot as below:21.JPG

 

 

 

Best regards,

Kris

 

 

 

 

Community Support Team _ Kris Dai
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

1 REPLY 1
Community Support Team
Community Support Team

Re: Json to Array Issue

Hi @MrRK,

 

Could you please show a bit more about the two SharePoint lists that you mentioned?

Further, could you please share a screenshot of your flow's configuration?

 

The error message told that the length() function expects its parameter to be an array or a string, but you provide an Object wihtin it. The length() function could not return the number of elements in an Object. More details about the length() function, please check the following article:

https://docs.microsoft.com/en-us/azure/logic-apps/logic-apps-workflow-definition-language#functions

 

I assume that you could get the corresponding content of the Product1 property within Ram json in your flow. If you want your flow to work well when there is one product, you should check if the Raw json is array or object firstly within your flow. If the Raw json is an object, you should convert the Object into an array at first.

 

I have made a test on my side and please take a try with the following workaround:

  • Add a proper trigger, here I use Flow Button trigger.
  • Add a Compose action, Inputs set to Raw json dynamic content from the HTTP request your side. On my side, Inputs set to following Raw json for testing:
{
  "Qty": "3",
  "Barcode": null,
  "Item": "Eggs",
  "Price": "3.50"
}
  • Add a Variables-> "Initialize variable" action, Name set to FinalArray, Type set to Array and Value set to empty.
  • Add a Variables-> "Initialize variable 2" action, Name set to InitializeArray, Type set to Array and Value set to output of "Compose" action.
  • Add a Parallel branch action, within left branch, add a "Set variable 2" action, Name choose FinalArray, Value set to output of "Compose" action. Within right branch, add a Scope action, within Scope action, add a "Compose 2" action, Inputs set to following formula:
array(outputs('Compose'))

Add a "Set variable" action, Name choose FinalArray variableValue set to output of "Compose 2" action. Then click "Menu" button of Scope action as below:14.JPG

Then click "Configure run after", within "Scope should run after" panel, select has failed option and click "Done".

 

  • Under the "Parallel branch" action, add a "Compose 3" action, Inputs set to following formula:
length(variables('FinalArray'))

Then click "Menu" button of Compose 3 action and then click "Configure run after", Within "Compose 3" action should run after panel, we firstly select Set variable 2 action tab, then select is successfull and is skipped option, click "Done" button:16.JPG

Then we select Scope action tab, select is successful option:17.JPG

 

 

Image reference:18.JPG

 

19.JPG

The flow works successfully as below:20.JPG

 

 

Within following "Apply to each" action, Input parameter should set to FinalArray variable, the screenshot as below:21.JPG

 

 

 

Best regards,

Kris

 

 

 

 

Community Support Team _ Kris Dai
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

Helpful resources

Announcements
thirdimage

Power Automate Community User Group Member Badge

Fill out a quick form to claim your user group badge now!

firstImage

Incoming: New and improved badges!

We've given our badges an overhaul and also added some brand new ones!

fifthimage

Microsoft Learn

Learn how to build the business apps that you need.

sixthImage

Power Platform World Tour

Find out where you can attend!

seventhimage

Webinars & Video Gallery

Watch & learn from the Power Automate Community Video Gallery!

Top Kudoed Authors
Users Online
Currently online: 193 members 5,421 guests
Please welcome our newest community members: