web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Help with building JSO...
Power Apps
Answered

Help with building JSON object up within a HTTP body

(0) ShareShare
ReportReport
Posted on by

I am trying to dynamically create a JSON object in the body of a HTTP request. The API I am calling does not like it when I include a field with a value set to an empty string " ". 

 

Take name for example, if this is empty then the API will throw an error. What I want is to replace the Name dynamic variable and replace with a custom expression which only includes the "name" : dyanamic_variable, when Name is not equal to " ". 

 

chrisrobbo_0-1670503135340.png

 

I have tried this :

if(equals(items('Apply_to_each_2')['name'], " "), "", ',"name": items('Apply_to_each_2')['name'],')

 

EDIT for clarification:

If name is not empty JSON looks like this...

{
    "name": dynamic_variable,
    "custom_fields" : {
      "ContactID": dynamic_variable,
      "AccountNumber": dynamic_variable,
      "CustomerGrade": dynamic_variable,
      "EnhancedGrade": dynamic_variable,
      "BranchName"dynamic_variable,
      "County"dynamic_variable,
      "OuterPostcode": dynamic_variable,
    }
}


where as if name is an empty string like so " ", then the JSON looks like this:

{
    "custom_fields" : {
      "ContactID": dynamic_variable,
      "AccountNumber": dynamic_variable,
      "CustomerGrade": dynamic_variable,
      "EnhancedGrade": dynamic_variable,
      "BranchName"dynamic_variable,
      "County"dynamic_variable,
      "OuterPostcode": dynamic_variable,
    }
}

 

 

Thanks!

Categories:
I have the same question (0)
  • CU07051257-0 Profile Picture
    on at

    Hi chrisrobbo,
    Perhaps you should try to use 'null' in the expression instead of " ".
    Like this:

     

     if(equals(items('Apply_to_each_2')['name'], null), "", ',"name": items('Apply_to_each_2')['name'],')

     

    I'm not sure, perhaps you should use this also for the result when true in the 'if' statement:

     

     if(equals(items('Apply_to_each_2')['name'], null), null, ',"name": items('Apply_to_each_2')['name'],')

     

    Hope this helps!
    Kind regards, Jan-Pieter

    PS. I think this question belongs on the PowerAutomate forum, but I don't know how to help changing that.

  • Sundeep_Malik Profile Picture
    6,484 on at

    Hey @Anonymous 

     

    I think here, coalesce function might help you. 

     

    To read more about it, I am giving you links:

     

    https://www.c-sharpcorner.com/article/usage-of-coalesce-function-in-power-automate/

    https://learn.microsoft.com/en-us/power-platform/power-fx/reference/function-isblank-isempty

     

    https://m.youtube.com/watch?v=bUoEjeEvDYE

     

    https://zeitgeistcode.com/power-automate-coalesce/

     

    So, for your case, I made a demo:

    Compose contains your data without name variable:

    Sundeep_Malik_0-1670504746081.png

     

    Then next is parse json, just to parse the json data:

    Sundeep_Malik_1-1670504775761.png

     

    Next I have a compose where I have your name variable and used coalesce function in it. I have passed null single quotes to it. 

    Expression looks like this:

    coalesce(body('Parse_JSON')?['name'],'')

    You can write anything after the comma and it will show that in output:

    Sundeep_Malik_2-1670504862361.png

     

    Final Output:

    Sundeep_Malik_3-1670504887628.png

     

    If you pass a false in it with this expression, it will show false: 

    coalesce(body('Parse_JSON')?['name'],false)

    Output:

    Sundeep_Malik_4-1670504976560.png

     

    And if I dont use coalesce function the output would be:

    Sundeep_Malik_5-1670505018634.png

     

     

    For your case write this expression:

     

    coalesce(items('Apply_to_each_2')?['name'],false)

    or

    coalesce(items('Apply_to_each_2')?['name'],'')

    I hope this will help. 🙂

    If it does, do accept this answer as the solution, so in future if anyone else has this kind of doubt he can refer to this post. 🙂

  • Verified answer
    Community Power Platform Member Profile Picture
    on at

    I was making an error, the API I am using does accept null values so as long as the data i provide from sql has null valus then I can use the answer from the below question

    https://powerusers.microsoft.com/t5/General-Power-Automate/Handling-NULL-in-Parse-JSON/td-p/40906

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 711 Most Valuable Professional

#2
Michael E. Gernaey Profile Picture

Michael E. Gernaey 319 Super User 2025 Season 2

#3
Power Platform 1919 Profile Picture

Power Platform 1919 268

Last 30 days Overall leaderboard