knowdotnet
knowdotnet

Reputation: 925

How to handle null or Non required column in json in Azure Logic App

enter image description here

sample api url: https://jsonplaceholder.typicode.com/posts

enter image description here

title is not a mandatory field in received json. It may or may not be part of each record.

When this field is missing in record, @{items('For_each')['title']} this throws an exception.

I want the value of myVariable to set as 'N/A' in that case. How do i do this?

Upvotes: 0

Views: 1697

Answers (2)

AdAstra
AdAstra

Reputation: 1984

I make the assumption that this is an array and that you have a set schema in the HTTP trigger. If the schema is set, make sure you remove Title as a required field. Required HTTP fields

Using these asumptions you should be able to do the following with Coalesce()

If Title now is not present in the body of the HTTP request the Title will be equal to 'N/A'

Logic App

Using postman to test, note, the result is backward as it is the first object sent in my array. Postman

Upvotes: 2

George Chen
George Chen

Reputation: 14344

Cause you url data all have the title, so I test with When a HTTP request is recived trigger to get the json data.

In this situation the data could only be like this:

{
    "userId": 1,
    "id": 2,

    "body": "xxxxx"
  }

not like the below one:

{
    "userId": 1,
    "id": 2,
    "title":,
    "body": "xxxxxxxxx"
  }

I test with the first one, it did show the error message: property 'title' doesn't exist,. So here is my solution, after the action Set variable, add an action to set the variable you want and set the action run after Set variable has failed like the below picture.

enter image description here

enter image description here

After configuration, if the title doesn't exist, it will be set as N/A as you want.

enter image description here

Hope this could help you, if this is not what you want or you have other questions, please let me know.

Upvotes: 1

Related Questions