Convert string into JSON in Logic Apps

Sebastian Tragust 25 Reputation points
2023-12-06T14:38:21.91+00:00

Hi everyone,

i have an array variable "vBody" in the following structure:

[
  {
     "headers": {
         "Content-Disposition": "form-data; name=\"file\"; filename=\"NAME1.pdf\""
     },
     "body": {
         "$Content-type": "application/pdf", 
         "$content": "CONTENT1"
     }   
  },   
  {
     "headers": {
         "Content-Disposition": "form-data; name=\"file\"; filename=\"NAME2.pdf\""
     },     
     "body": {
         "$Content-type": "application/pdf",
         "$content": "CONTENT2"
     }   
  },   
  {
     "headers": {
         "Content-Disposition": "form-data; name=\"file\"; filename=\"NAME3.pdf\""
     }, 
     "body": {
         "$Content-type": "application/pdf",
         "$content": "CONTENT3"
     }
  }
]

I then use join(variables('vBody'), ',') on the array and get the following string:

{"headers":{"Content-Disposition":"form-data; name=\"file\"; filename=\"NAME1.pdf\""},"body":{"$Content-type":"application/pdf","$content":"CONTENT1"}},{"headers":{"Content-Disposition":"form-data; name=\"file\"; filename=\"NAME2.pdf\""},"body":{"$Content-type":"application/pdf","$content":"CONTENT2"}},{"headers":{"Content-Disposition":"form-data; name=\"file\"; filename=\"NAME3.pdf\""},"body":{"$Content-type":"application/pdf","$content":"CONTENT3"}}

I then want to convert the whole string into a JSON with json(outputs('TEST_Compose_join')). But when I do that I get the following raw output:

{
    "headers": {
        "Content-Disposition": "form-data; name=\"file\"; filename=\"NAME1.pdf\""
    },
    "body": {
        "$Content-type": "application/pdf",
        "$content": "CONTENT1"
    }
}
  • Why do I only get the output of the first element (filename = NAME1)?
  • Where are the other two elements in the JSON (filename = NAME2 & NAME3)?
  • How can I convert the string into a JSON with the elements/contents of all the files in it?

My Logic App Workflow looks like this:

Screenshot 2023-12-06 152922

Ultimately, I want to add the whole JSON (with all the contents of the different files) to a HTTP PUT action.

Azure Logic Apps
Azure Logic Apps
An Azure service that automates the access and use of data across clouds without writing code.
3,542 questions
0 comments No comments
{count} votes

Accepted answer
  1. Amira Bedhiafi 33,071 Reputation points Volunteer Moderator
    2023-12-09T17:56:58.5266667+00:00

    When you use the join() function to combine an array of objects, and then try to convert it back into JSON, it's crucial to ensure that the format of the string is valid JSON. From your description, it seems like the issue might be with the way the string is being formed after using the join() function.

    When you use join(variables('vBody'), ','), it concatenates the array elements into a single string, but this string is not enclosed within square brackets [], which are necessary for it to be recognized as a JSON array. You should modify this to:

    
       "[" + join(variables('vBody'), ',') + "]"
    
    

    After ensuring the correct format, you can then use json() to convert the string back to a JSON object.

    
       json(outputs('Compose_step_with_modified_join'))
    
    

    Replace 'Compose_step_with_modified_join' with the appropriate action name where you have the modified join result.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.