How to filter unnecessary data from MSN Weather API response body in logic app?

Teemu Roponen 25 Reputation points
2023-06-11T14:22:37.5233333+00:00

Hello,

I'm trying to create a logic app workflow that receives list of cities as a HTTP request and returns the location, temperature and conditions in each of the cities in JSON format. I don't know how to filter the results.

Here is a picture of my workflow:

User's image

And here is the test i'm running and the response i'm getting in Postman:

User's image

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

Accepted answer
  1. Sonny Gillissen 3,351 Reputation points
    2023-06-11T20:34:35.81+00:00

    Hi Teemu Roponen

    Thanks for reaching out on Microsoft Q&A!

    I've tested it in my test lab, and I think in your case I think it's best to use a 'Select' action. This type of action basically does a loop over an array, making it possible to select values from that array and refer them to new attributes in a newly defined JSON easily.

    Please note that my screenshots are still in the "old" designer, where yours are in the "new" designer. Nevertheless, the expressions and snippets will work on your end as well.

    So, first you'll need an array. Since your 'Get current weather' action is placed within a 'For each' loop, you can directly refer to the body of that action outside of the loop, turning it automatically into an array of all bodies within the loop. You can do this by adding the following expression in the 'From' field:

    body('Get_current_weather')

    Next, you want to assign new attributes, and use the values of the weather response body to form a new JSON trimmed the way you want it. When looking at the response of MSN Weather it's kinda "complex", in that sense: it contains a lot of objects within objects. Therefor you need to point to the actual value you need, using the expressions below:

    • Location:
      item()['responses']['source']['location']
    • Condition:
      item()['responses']['weather']['current']['cap']
    • Temperature:
      concat(item()['responses']['weather']['current']['temp'],item()['Units']['temperature'])

    In my last code snippet, the one for temperature, I've used "concat" to paste two values together, being: temperature and unit of temperature, so it returns "22‎°C" instead of just the number "22‎". If you don't want this, and only want the number, you can simply use this code:

    item()['responses']['weather']['current']['temp']

    Lastly, you can set the body of your 'Response' action to contain the output of the 'Select' action, now making your flow look somewhat like below:

    User's image

    Hope this helps! Please click 'Accept answer' if it did. Feel free to drop addtional queries in the comments below :-)

    Kind regards,

    Sonny


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.