Failure in Parse JSON due to different tags

406 views Asked by At

I am extremely new to JSON so please bear with me if this is a super basic question. I am using Microsoft Power Automate/Flow to build a flow dealing with an incoming JSON file. I placed a Parse JSON control in the flow and used a sample export from the solution coming in and the schema was built for me and ran just fine, no errors. The problem came up when I had a JSON file come in with a different tag than what the schema was looking for so it failed on me. This is from SurveyMonkey and the field that is causing me problems is not listed in the schema. The schema that was built for me looks like this:

{
  "type": "array",
  "items": {
    "type": "object",
    "properties": {
      "id": {
        "type": "string"
      },
      "answers": {
        "type": "array",
        "items": {
          "type": "object",
          "properties": {
            "choice_id": {
              "type": "string"
            },
            "simple_text": {
              "type": "string"
            }
          },
          "required": [
            "choice_id",
            "simple_text"
          ]
        }
      },
      "family": {
        "type": "string"
      },
      "subtype": {
        "type": "string"
      },
      "heading": {
        "type": "string"
      }
    },
    "required": [
      "id",
      "answers",
      "family",
      "subtype",
      "heading"
    ]
  }
}

The error I get is "Required properties are missing from object: choice_id."

For the scenario that the response coming in has a comment on it, the tag is other_id vs. choice_id. Is there a way to set the JSON schema to see that other_id as an optional tag and run if it exists or not?

Here is a snippet of the JSON file that I am getting in with the optional comment:

      {
        "choice_id": "10054397857",
        "row_id": "10054397873",
        "choice_metadata": {
          "weight": "4"
        },
        "simple_text": "Fixed question answer"
      },
      {
        "other_id": "10054397859",
        "text": "open and optional comment",
        "tag_data": [
          {
            "hexcolor": "F9BE00",
            "label": "sm_neutral",
            "tag_type": "sentiment"
          }
        ],
        "simple_text": "open and optional comment"
      }
1

There are 1 answers

0
Jayson Larner On

Found the issue and it just required changing the JSON Schema slightly. See below for the new sample Schema that is working with several tests against it. The changes required adding an additional properties option, adding a new element to the required area and changing required to optional.

{
    "type": "array",
    "items": {
        "type": "object",
        "properties": {
            "id": {
                "type": "string"
            },
            "answers": {
                "type": "array",
                "items": {
                    "type": "object",
                    "properties": {
                        "choice_id": {
                            "type": "string"
                        },
                        "other_id": {
                            "type": "string"
                        },
                        "simple_text": {
                            "type": "string"
                        }
                    },
                    "optional": [
                        "choice_id",
                        "other_id",
                        "simple_text"
                    ]
                }
            },
            "family": {
                "type": "string"
            },
            "subtype": {
                "type": "string"
            },
            "heading": {
                "type": "string"
            }
        },
        "required": [
            "id",
            "answers",
            "family",
            "subtype",
            "heading"
        ]
    }
}