3

Im trying a setup a Microsoft flow. In short, I need to take JSON data retrieved from a device, and parse it so that i could reference it in the Flows below. In order to parse, i need to provide the JSON Schema to Flow. Microsoft Flow has an option to generate it from a sample payload (the results returned from the API call), but it's not generating it correctly. I'm hoping someone can help me. I need the correct JSON Schema.

The data returned from the API:

[
  null,
  [
    {
      "user_id": 2003,
      "user_label": "Test1"
    },
    {
      "user_id": 2004,
      "user_label": "Test2"
    }
  ]
]

Scheme generated in Flow from the above sample payload:

{
    "type": "array",
    "items": {}
}

I then tried to generate the Schema from just the data. That seemed to work, but when the Flow runs, I get a Json validation error.

Tried generating from just the data like this:

{
      "user_id": 2003,
      "user_label": "Test1"
    }

This generated the scheme like this:

{
    "type": "object",
    "properties": {
        "user_id": {
            "type": "number"
        },
        "user_label": {
            "type": "string"
        }
    }
}
Ambrose Leung
  • 3,704
  • 2
  • 25
  • 36
Joe
  • 165
  • 2
  • 13

1 Answers1

0

So you have 2 things going on, the nested object array, and the null.

You'll need another Parse JSON after the first Parse JSON. And you'll want to filter out the null before the second Parse JSON.

It took me a while to figure out, but I hope this helps.

Start by adding the Parse JSON step to whatever step is outputting the JSON. first parse json

Now, filter the array, make sure you use the 'Expression' when comparing with null. enter image description here

Add the second Parse JSON, you'll notice that you won't have the option to select the output "Item" of the Filter array step, so select 'Parse JSON' - Item for now (we will change this to use the output of the Filter JSON step in a moment) enter image description here

The step should automatically change to an 'Apply to each'. In the Parse JSON 2, generate the schema with

[
    {
      "user_id": 2003,
      "user_label": "Test1"
    },
    {
      "user_id": 2004,
      "user_label": "Test2"
    }
  ]

Then, modify the 'Select an output from previous steps field' and change it (from the Body of the Parse JSON step) to the Body of the Filter Array step

enter image description here

Finally, add an action after Parse JSON 2 and select one of the fields in Parse JSON 2, this will automatically change that step to a nested Apply to each

enter image description here

You should end up with something like this: enter image description here

Ambrose Leung
  • 3,704
  • 2
  • 25
  • 36