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

Convert JSON string to JSON array

I've got a system that handles some user input and for questions with multiple checkboxes, it stores the value as a string with a pipe delimiter. We need to transfer some of this data from this one system to a secondary system. Structure of the first system is fixed and can't be changed so I'm trying to use Flow to handle some of the data conversion in the middle
 
Here's how the data would be POSTed to Flow in key:value pair.
 
"components": "Value A | Value B | Value C"
 
Using Flow, I've been able to use the split() function to take this value and convert it to the following:
 
[
    "Value A",
    "Value B",
    "Value C"
]
 
This is where I get stuck. I need the array to be arranged as a JSON key:value pairing. It would need to look like this before I can transfer the JSON to a secondary system. How do I get the above string or array to output like below?
 
[
            {
                "name": "Value A"
            },
            {
                "name": "Value B"
            },
            {
                "name": "Value C"
            }
        ],
1 ACCEPTED SOLUTION

Accepted Solutions
cmiller2784
Level: Powered On

Re: Convert JSON string to JSON array

Was able to fix my own issue using a combination of "Apply to Each" and the item() expression. I was able to output the value as follows:

[{"name":"Value A"},{"name":"Value B"},{"name":"Value C"}]

 

Step 1: Take the string, use split() to convert the string into an array based on the delimiter.

Step 2: Create an array variable with no value.

Step 3: Using the "Apply to each" control, take the Compose output from Step 1 and input the key pairing I needed, "name" in this case. The item() function returns an array object for each iteration when used with "Apply to each". 

Step 3A. Append the output from Compose 2 to the Array Variable. 

 

Here's a screenshot of the flow:

2018-12-05_17-31-13.jpg

View solution in original post

3 REPLIES 3
cmiller2784
Level: Powered On

Re: Convert JSON string to JSON array

Was able to fix my own issue using a combination of "Apply to Each" and the item() expression. I was able to output the value as follows:

[{"name":"Value A"},{"name":"Value B"},{"name":"Value C"}]

 

Step 1: Take the string, use split() to convert the string into an array based on the delimiter.

Step 2: Create an array variable with no value.

Step 3: Using the "Apply to each" control, take the Compose output from Step 1 and input the key pairing I needed, "name" in this case. The item() function returns an array object for each iteration when used with "Apply to each". 

Step 3A. Append the output from Compose 2 to the Array Variable. 

 

Here's a screenshot of the flow:

2018-12-05_17-31-13.jpg

View solution in original post

TOPDHI
Level: Powered On

Re: Convert JSON string to JSON array

Too long...

 

1, Init array variable

2, do ParseJSON for your JSON in string

3, Set body from ParseJSON outcome directly as an array value in SetVariable you created in step 1.

 

No need to apply to each and additional clutter. Works for me. 

Tomas

Highlighted
cmiller2784
Level: Powered On

Re: Convert JSON string to JSON array

Except that method doesn't give me what I'm looking for. Your solution only supplies an array without the necessary JSON key, it just supplies the value. 

Helpful resources

Announcements
firstImage

Better Together Contest Finalists Announced!

Congrats to the finalists of our ‘Better Together’-themed T-shirt design contest! Click for the top entries.

firstImage

Incoming: New and improved badges!

Look out for new contribution recognition badges coming SOON!

firstImage

New & Improved Power Automate Community Cookbook

We've updated and improved the layout and uploading format of the Power Automate Cookbook!

thirdimage

Power Automate Community User Group Member Badge

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

sixthImage

Community Summit North America

The top training and networking event across the globe for Microsoft Business Applications

Top Solution Authors
Top Kudoed Authors
Users online (9,025)