0

Looking forward to an approach to convert simple JSON objects to Key value pair JSON objects using Logic Apps. Like:

{
            "FirstName": "ABC",
            "LastName": "123",
            "MiddleName": null,
}

to

  [
    {
        "Key": "FirstName"
        "Value": "ABC"
    },
    {
        "Key": "LastName"
        "Value": "123"
    }
 ]

Thanks for the suggestions.

Ikhtesam Afrin
  • 897
  • 1
  • 1
  • 6
Deepak Shaw
  • 461
  • 3
  • 6
  • Yep … https://learn.microsoft.com/en-us/connectors/advanceddataoperatio/, more specifically … https://www.statesolutions.com.au/json-properties-to-name-value-pair-array/ – Skin Jun 14 '23 at 07:24

2 Answers2

0

I have followed the below steps to convert JSON Object to Key-Value pair JSON Objects-

  1. Created one "When a HTTP request is received" as below

enter image description here

  1. Add a Parse Json action. I have taken triggerBody() in the Content field

enter image description here

Here Schema is

{
"properties": {
"FirstName": {
"type": "string"
},
"LastName": {
"type": "string"
},
"MiddleName": {
"type": "string"
}
},
"type": "object"
}
  1. After the Parse JSON action, add a Compose action. Enter the following expression in the Inputs field of the Compose action

     [
     {
             "Key": "FirstName",
             "Value": "@{body('Parse_JSON')['FirstName']}"
     },
     {
             "Key": "LastName",
             "Value": "@{body('Parse_JSON')['LastName']}"
     },
     {
             "Key": "MiddleName",
             "Value": "@{body('Parse_JSON')['MiddleName']}"
     }
     ]
    

enter image description here

  1. Save and Run the logic app.

Output-

enter image description here

enter image description here

Check in the Runs History

enter image description here

enter image description here

Ikhtesam Afrin
  • 897
  • 1
  • 1
  • 6
  • Hi, Thanks for your reply.. But it won't work if a new node gets added in the inbound message like: { "FirstName": "ABC", "LastName": "123", "MiddleName": null, "Age": 30 } then expected outputs would be: [ { "Key": "FirstName" "Value": "ABC" }, { "Key": "LastName" "Value": "123" }, { "Key": "Age" "Value": 30 } ] – Deepak Shaw Jun 14 '23 at 23:36
  • try referring this SO question https://stackoverflow.com/questions/63795368/is-there-a-way-to-get-json-keys-without-using-inline-js – Ikhtesam Afrin Jun 15 '23 at 10:59
0

As per my comment, you should (could) look at using the Advanced Data Operations connector, it has an operation which is specifically designed to complete your requirement.

https://learn.microsoft.com/en-us/connectors/advanceddataoperatio/

https://www.statesolutions.com.au/json-properties-to-name-value-pair-array/

Flow

This is the resulting JSON it will give back ...

[
  {
    "propertyName": "FirstName",
    "propertyType": "String",
    "propertyValue": "ABC"
  },
  {
    "propertyName": "LastName",
    "propertyType": "String",
    "propertyValue": "123"
  },
  {
    "propertyName": "MiddleName",
    "propertyType": "Null",
    "propertyValue": null
  }
]
Skin
  • 9,085
  • 2
  • 13
  • 29