Use a schema for the minimum data you require
  • Updated on 22 Sep 2019
  • 3 minutes to read
  • Contributors
  • Print
  • Comments
  • Share
  • Dark
    Light

Use a schema for the minimum data you require

  • Print
  • Comments
  • Share
  • Dark
    Light

#ServerlessTips - Microsoft Flow
Author: Kent Weare Integration MVP

A tip I have found very useful when working with Json and Json Schema with Logic Apps is to focus on just the data you need rather than trying to do a big schema covering everything.
If we tale the below example message which is a product update from Shopify. This is a pretty big message with a few collection properties in it.

{
  "id": 2241851457595,
  "title": "1 to 6T Toddler Baby Girls Clothes",
  "body_html": "<ul></ul>\n<ul>\n<li>\n<strong>Gender:</strong> Girls</li>\n<li><span><strong>Sleeve Length(cm):</strong> Short</span></li>\n<li><span><strong>Silhouette:</strong> A-Line</span></li>\n<li><span><strong>Fit:</strong> Fits true to size, take your normal size</span></li>\n<li><span><strong>Style:</strong> Casual</span></li>\n<li>\n<strong>Material:</strong> Polyester,Cotton</li>\n<li><span><strong>Sleeve Style:</strong> Regular</span></li>\n<li><span><strong>Model Number:</strong> Clothes Set</span></li>\n<li>\n<strong>Dresses Length:</strong> Above Knee, Mini</li>\n<li><span><strong>Decoration:</strong> Pattern</span></li>\n<li>\n<strong>Collar:</strong> O-neck</li>\n</ul>\n<p> </p>\n<p> </p>\n<p> </p>\n<p> </p>\n<p> </p>",
  "vendor": "Nothing but Unicorns",
  "product_type": "",
  "created_at": "2019-05-12T18:19:09+01:00",
  "handle": "1-to-6t-toddler-baby-girls-clothes",
  "updated_at": "2019-06-05T08:59:13+01:00",
  "published_at": "2019-05-12T18:19:08+01:00",
  "template_suffix": "",
  "tags": "dress",
  "published_scope": "web",
  "variants": [
    {
      "id": 20991681855547,
      "product_id": 2241851457595,
      "title": "as picture / 12M",
      "price": "22.99",
      "sku": "17051277-as-picture-12m",
      "position": 1,
      "inventory_policy": "deny",
      "compare_at_price": "22.99",
      "fulfillment_service": "oberlo",
      "inventory_management": "shopify",
      "option1": "as picture",
      "option2": "12M",
      "option3": null,
      "created_at": "2019-05-12T18:19:09+01:00",
      "updated_at": "2019-05-12T18:19:15+01:00",
      "taxable": true,
      "barcode": null,
      "grams": 0,
      "image_id": 8310233399355,
      "weight": 0.0,
      "weight_unit": "lb",
      "inventory_item_id": 21593991774267,
      "inventory_quantity": 992,
      "old_inventory_quantity": 992,
      "requires_shipping": true
    },
    {
      "id": 20991681888315,
      "product_id": 2241851457595,
      "title": "as picture / 2T",
      "price": "22.99",
      "sku": "17051277-as-picture-2t",
      "position": 2,
      "inventory_policy": "deny",
      "compare_at_price": "22.99",
      "fulfillment_service": "oberlo",
      "inventory_management": "shopify",
      "option1": "as picture",
      "option2": "2T",
      "option3": null,
      "created_at": "2019-05-12T18:19:09+01:00",
      "updated_at": "2019-05-12T18:19:15+01:00",
      "taxable": true,
      "barcode": null,
      "grams": 0,
      "image_id": 8310233399355,
      "weight": 0.0,
      "weight_unit": "lb",
      "inventory_item_id": 21593991839803,
      "inventory_quantity": 992,
      "old_inventory_quantity": 992,
      "requires_shipping": true
    },    
  ],
  "options": [
    {
      "id": 3106363703355,
      "product_id": 2241851457595,
      "name": "Color",
      "position": 1,
      "values": [
        "as picture"
      ]
    },
    {
      "id": 3106363736123,
      "product_id": 2241851457595,
      "name": "Kid Size",
      "position": 2,
      "values": [
        "12M",
        "2T",
        "3T",
        "4T",
        "5"
      ]
    }
  ],
  "images": [
    {
      "id": 8310233301051,
      "product_id": 2241851457595,
      "position": 1,
      "created_at": "2019-05-12T18:19:11+01:00",
      "updated_at": "2019-05-12T18:19:11+01:00",
      "alt": null,
      "width": 800,
      "height": 800,
      "src": "https://cdn.shopify.com/s/files/1/0052/5598/7259/products/product-image-701552965.jpg?v=1557681551",
      "variant_ids": []
    },
    {
      "id": 8310233399355,
      "product_id": 2241851457595,
      "position": 2,
      "created_at": "2019-05-12T18:19:13+01:00",
      "updated_at": "2019-05-12T18:19:13+01:00",
      "alt": null,
      "width": 640,
      "height": 640,
      "src": "https://cdn.shopify.com/s/files/1/0052/5598/7259/products/product-image-701552977.jpg?v=1557681553",
      "variant_ids": [
        20991681855547,
        20991681888315,
        20991681921083,
        20991681953851,
        20991681986619
      ]
    }
  ],
  "image": {
    "id": 8310233301051,
    "product_id": 2241851457595,
    "position": 1,
    "created_at": "2019-05-12T18:19:11+01:00",
    "updated_at": "2019-05-12T18:19:11+01:00",
    "alt": null,
    "width": 800,
    "height": 800,
    "src": "https://cdn.shopify.com/s/files/1/0052/5598/7259/products/product-image-701552965.jpg?v=1557681551",
    "variant_ids": []
  }
}

In the above modified Json I have got rid of everything except the properties I need. This means that when the json is parsed it will be quicker and parse just the required data.

For the collections in the message I have just left them as empty json arrays and I’ll come back to these later. Using the message parsed against the above schema I can easily update the SQL table for the main product data.

Next, I will do a loop in the Logic App and supply the property from the variants.

Inside the loop the first thing I will do is use the parse Json shape on the body of the variant array. I will define a schema using the json from inside the variant array which would look like the following:

{
      "id": 20991681855547,
      "product_id": 2241851457595,
      "title": "as picture / 12M",
      "price": "22.99",
      "sku": "17051277-as-picture-12m",
      "position": 1,
      "inventory_policy": "deny",
      "compare_at_price": "22.99",
      "fulfillment_service": "oberlo",
      "inventory_management": "shopify",
      "option1": "as picture",
      "option2": "12M",
      "option3": null,
      "created_at": "2019-05-12T18:19:09+01:00",
      "updated_at": "2019-05-12T18:19:15+01:00",
      "taxable": true,
      "barcode": null,
      "grams": 0,
      "image_id": 8310233399355,
      "weight": 0.0,
      "weight_unit": "lb",
      "inventory_item_id": 21593991774267,
      "inventory_quantity": 992,
      "old_inventory_quantity": 992,
      "requires_shipping": true
    }

When I’m executing my code within the loop by using this sub-schema, I am focusing on the property/array that is the variants.

This smaller focus on the use of schemas makes it much easier to develop my Logic App without as many random data errors. Because, there are inferred conditions in the data that I either don’t care about or can catch and fix quicker.

Was this article helpful?