cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Robin-Cr
Frequent Visitor

Parse Json multi nested

Hi!

 

I am having some trouble with converting my HTTP get connector to a working JSON. The ultimate goal is to store the data in a database. 

 

The raw data looks like:

{
    "2023-01-01 00:15+01:00": [
        {
            "unit""kWh",
            "type""E",
            "rate""low",
            "direction""consumption",
            "value"30.0
        },
        {
            "unit""kWh",
            "type""E",
            "rate""normal",
            "direction""consumption",
            "value"0
        }
    ],
    "2023-01-01 00:30+01:00": [
        {
            "unit""kWh",
            "type""E",
            "rate""low",
            "direction""consumption",
            "value"31.0
        },
        {
            "unit""kWh",
            "type""E",
            "rate""normal",
            "direction""consumption",
            "value"0
        }
    ],
    "2023-01-01 00:45+01:00": [
        {
            "unit""kWh",
            "type""E",
            "rate""low",
            "direction""consumption",
            "value"32.0
        },
        {
            "unit""kWh",
            "type""E",
            "rate""normal",
            "direction""consumption",
            "value"0
        }
    ]
}
 
I have tried a few things, however i keep getting errors saying the values return a NULL or not in a valid array. In this case each date has his own values. It has 2 x the same values. One for normal rate and one of Low rates. 
I am really only looking for the one that is filled. So if the low value is filled insert that in a database, if normal is filled fill that one in the database. However i am already having issues with insert all of it in the database. 
 
Does anyone have a solution to parse the above values in a database?
 
Thanks in advance.
21 REPLIES 21
grantjenkins
Super User
Super User

Attempt number 20 😉.

 

See full flow below. I'll go into each of the actions.

grantjenkins_0-1676638890875.png

 

JSON is a Compose that contains your data.

grantjenkins_1-1676638920351.png

 

Select uses the output from JSON and uses the following expressions:

//From
split(replace(string(outputs('JSON')), ']', '['), '[')

//Map
item()

grantjenkins_2-1676638999090.png

 

Filter array uses the output from Select and uses the following expression.

item()

/The actual filter expression is:
@not(startsWith(item(), '}'))

grantjenkins_3-1676639082778.png

 

Initialize variable Items creates an Array variable called items that will eventually contain the items we want.

grantjenkins_4-1676639127215.png

 

Apply to each iterates over the items in our Filter array in chunks of 2 (2 items at a time). The first item is the date and the second item is the actual objects.

chunk(body('Filter_array'), 2)

grantjenkins_5-1676639214059.png

 

Compose uses the following expression to get the objects in JSON format.

json(concat('{"values":[', last(item()), ']}'))

grantjenkins_6-1676639284271.png

 

Filter array Non Zero filters our data so only the items where value is greater than zero are kept.

//From
outputs('Compose')?['values']

//Filter
item()?['value']

//The full filter expression is:
@greater(item()?['value'], 0)

grantjenkins_7-1676639396804.png

 

Append to array variable Items adds the object to the array while also adding the date property. Because the dates are ISO dates, we can safely assume the length will always be the same regardless of date and time.

addProperty(first(body('Filter_array_Non_Zero')), 'date', slice(first(item()), 2, 24))

grantjenkins_8-1676639470582.png

 

That should give you all the objects within the Items array variable. After the Apply to each, I've added another Compose (Output) that just shows the contents of the items array.

grantjenkins_9-1676639577279.png

 

And the final output:

[
  {
    "unit": "kWh",
    "type": "E",
    "rate": "normal",
    "direction": "consumption",
    "value": 30,
    "date": "2023-01-01 00:15+01:00"
  },
  {
    "unit": "kWh",
    "type": "E",
    "rate": "low",
    "direction": "consumption",
    "value": 31,
    "date": "2023-01-01 00:30+01:00"
  },
  {
    "unit": "kWh",
    "type": "E",
    "rate": "low",
    "direction": "consumption",
    "value": 32,
    "date": "2023-01-01 00:45+01:00"
  }
]

grantjenkins_10-1676639628320.png

 


----------------------------------------------------------------------
If I've answered your question, please mark the post as Solved.
If you like my response, please consider giving it a Thumbs Up.
Robin-Cr
Frequent Visitor

@grantjenkins Thank you so much, that worked! Thanks for your time.

Helpful resources

Announcements

Hear what's next for the Power Up Program

Hear from Principal Program Manager, Dimpi Gandhi, to discover the latest enhancements to the Microsoft #PowerUpProgram, including a new accelerated video-based curriculum crafted with the expertise of Microsoft MVPs, Rory Neary and Charlie Phipps-Bennett. If you’d like to hear what’s coming next, click the link below to sign up today! https://aka.ms/PowerUp  

Check out the Copilot Studio Cookbook today!

We are excited to announce our new Copilot Cookbook Gallery in the Copilot Studio Community. We can't wait for you to share your expertise and your experience!    Join us for an amazing opportunity where you'll be one of the first to contribute to the Copilot Cookbook—your ultimate guide to mastering Microsoft Copilot. Whether you're seeking inspiration or grappling with a challenge while crafting apps, you probably already know that Copilot Cookbook is your reliable assistant, offering a wealth of tips and tricks at your fingertips--and we want you to add your expertise. What can you "cook" up?   Click this link to get started: https://aka.ms/CS_Copilot_Cookbook_Gallery   Don't miss out on this exclusive opportunity to be one of the first in the Community to share your app creation journey with Copilot. We'll be announcing a Cookbook Challenge very soon and want to make sure you one of the first "cooks" in the kitchen.   Don't miss your moment--start submitting in the Copilot Cookbook Gallery today!     Thank you,  Engagement Team

Tuesday Tip | How to Report Spam in Our Community

It's time for another TUESDAY TIPS, your weekly connection with the most insightful tips and tricks that empower both newcomers and veterans in the Power Platform Community! Every Tuesday, we bring you a curated selection of the finest advice, distilled from the resources and tools in the Community. Whether you’re a seasoned member or just getting started, Tuesday Tips are the perfect compass guiding you across the dynamic landscape of the Power Platform Community.   As our community family expands each week, we revisit our essential tools, tips, and tricks to ensure you’re well-versed in the community’s pulse. Keep an eye on the News & Announcements for your weekly Tuesday Tips—you never know what you may learn!   Today's Tip: How to Report Spam in Our Community We strive to maintain a professional and helpful community, and part of that effort involves keeping our platform free of spam. If you encounter a post that you believe is spam, please follow these steps to report it: Locate the Post: Find the post in question within the community.Kebab Menu: Click on the "Kebab" menu | 3 Dots, on the top right of the post.Report Inappropriate Content: Select "Report Inappropriate Content" from the menu.Submit Report: Fill out any necessary details on the form and submit your report.   Our community team will review the report and take appropriate action to ensure our community remains a valuable resource for everyone.   Thank you for helping us keep the community clean and useful!

Users online (2,758)