Parse Json - Dont Over-complicate the Schema
  • Updated on 25 Jan 2020
  • 3 minutes to read
  • Contributors
  • Print
  • Comments
  • Share
  • Dark
    Light

Parse Json - Dont Over-complicate the Schema

  • Print
  • Comments
  • Share
  • Dark
    Light

Sometimes in Logic App development we need to use the Parse Json action to parse json and get some nice strongly typed properties to work with in our Logic App. The parse json action is great where you can just paste a sample json message and it will generate a json schema for you and give you all of these properties for very little effort.

This is great but one of the real world problems is that messages are often more complicated and sometimes the generated schema will get an error because the data was more complex than you anticipated and you hadnt covered a scenario.

I think when using the Parse Json action its a good tip to focus on just the data you need rather than building a schema for the entire message when you dont need most of it.

Scenario

Lets imagine we have a json like below which describes an invoice for an order. We want to be able to process the invoice in a Logic App but the required process needs us to make some logic decisions. Lets say i need to do the following:

  • Receive the order over http
  • Extract the invoice number
  • If the order is more than 100 GBP then I need to raise it for
{
  "Invoice": {
    "Invoice": {
      "id": "123",
      "InvoiceHeader": {
        "InvoiceNumber": "123",
        "InvoiceDate": "1999-03-13",
        "InvoiceType": "Goods",
        "Total": "123.5",
        "SubmittedTotal": "123.5",
        "TotalLineItems": "2",
        "CurrencyCode": "GBP"
      },
      "InvoiceDetails": {
        "LineItem": [
          {
            "LineItemNumber": "1",
            "Quantity": "1",
            "Units": "ea",
            "UnitPrice": "100",
            "Description": "Test",
            "Discount": {
              "Total": "0",
              "RateOrPercentage": "0"
            },
            "SubTotal": "100"
          },
          {
            "LineItemNumber": "1",
            "Quantity": "1",
            "Units": "ea",
            "UnitPrice": "100",
            "Description": "Test",
            "Discount": {
              "Total": "0",
              "RateOrPercentage": "0"
            },
            "SubTotal": "100"
          }
        ]
      }
    }
  }
}

Using Parse Json with the above message would produce the below schema.

{
    "type": "object",
    "properties": {
        "Invoice": {
            "type": "object",
            "properties": {
                "Invoice": {
                    "type": "object",
                    "properties": {
                        "id": {
                            "type": "string"
                        },
                        "InvoiceHeader": {
                            "type": "object",
                            "properties": {
                                "InvoiceNumber": {
                                    "type": "string"
                                },
                                "InvoiceDate": {
                                    "type": "string"
                                },
                                "InvoiceType": {
                                    "type": "string"
                                },
                                "Total": {
                                    "type": "string"
                                },
                                "SubmittedTotal": {
                                    "type": "string"
                                },
                                "TotalLineItems": {
                                    "type": "string"
                                },
                                "CurrencyCode": {
                                    "type": "string"
                                }
                            }
                        },
                        "InvoiceDetails": {
                            "type": "object",
                            "properties": {
                                "LineItem": {
                                    "type": "array",
                                    "items": {
                                        "type": "object",
                                        "properties": {
                                            "LineItemNumber": {
                                                "type": "string"
                                            },
                                            "Quantity": {
                                                "type": "string"
                                            },
                                            "Units": {
                                                "type": "string"
                                            },
                                            "UnitPrice": {
                                                "type": "string"
                                            },
                                            "Description": {
                                                "type": "string"
                                            },
                                            "Discount": {
                                                "type": "object",
                                                "properties": {
                                                    "Total": {
                                                        "type": "string"
                                                    },
                                                    "RateOrPercentage": {
                                                        "type": "string"
                                                    }
                                                }
                                            },
                                            "SubTotal": {
                                                "type": "string"
                                            }
                                        },
                                        "required": [
                                            "LineItemNumber",
                                            "Quantity",
                                            "Units",
                                            "UnitPrice",
                                            "Description",
                                            "Discount",
                                            "SubTotal"
                                        ]
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

Straight away you can see that is a big schema and there could be the following potential problems:

  • Any fields which are optional but the generated schema has assumed will be there (required) will cause the parse to fail
  • In cases where you might have converted from XML to JSON you might have issues with arrays being objects if there is only 1 in the array
  • You might make bad assumptions about the data types

In the scenario here I only need two of the properties (Total and InvoiceNumber). I can make a much simpler example message by removing all of the stuff I dont care about. Like for example below.

{
  "Invoice": {
    "Invoice": {
      "InvoiceHeader": {
        "InvoiceNumber": "123",        
        "Total": "123.5"
      }
    }
  }
}

When I paste this into Parse Json to generate a schema I now get a much simpler schema, see below.

{
    "type": "object",
    "properties": {
        "Invoice": {
            "type": "object",
            "properties": {
                "Invoice": {
                    "type": "object",
                    "properties": {
                        "InvoiceHeader": {
                            "type": "object",
                            "properties": {
                                "InvoiceNumber": {
                                    "type": "string"
                                },
                                "Total": {
                                    "type": "string"
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

When the Logic App runs, no data is lost but the schema will just parse the bits relevant to my schema and give me the properties i need to make decisions on in the Logic App but the parse will not fail if one of the other properties isnt as I expected.

This is a simple tip and hopefully one that can save you a bit of grief.

Was this article helpful?