3

I have managed to get all my userdata in an array (see here) but now I cannot loop through the data. After building the array I have converted it to JSON, but I can no longer address the fields as defined in my JSON schema.

The only thing I can address in my loop (I use the JSON body as input for the For Each loop) is the body itself, not the individual fields like username, mail address etc.

Should I change something in my JSON schema to overcome this or is something else wrong?

Edit: Please find my JSON schema below:

   {
       "$schema": "http://json-schema.org/draft-04/schema#",
       "items": [
           {
               "properties": {
                   "@@odata.type": {
                       "type": "string"
                   },
                   "createdDateTime": {
                       "type": "string"
                   },
                   "employeeId": {
                       "type": "string"
                   },
                   "givenName": {
                       "type": "string"
                   },
                   "id": {
                       "type": "string"
                   },
                   "mail": {
                       "type": "string"
                   },
                   "onPremisesSamAccountName": {
                       "type": "string"
                   },
                   "surname": {
                       "type": "string"
                   },
                   "userPrincipalName": {
                       "type": "string"
                   }
               },
               "required": [
                   "@@odata.type",
                   "id",
                   "givenName",
                   "surname",
                   "userPrincipalName",
                   "mail",
                   "onPremisesSamAccountName",
                   "employeeId",
                   "createdDateTime"
               ],
               "type": "object"
           }
       ],
       "type": "array"
   }

Please see the image for how the JSON looks:

JSON Schema

Martijn Balink
  • 35
  • 1
  • 1
  • 5

1 Answers1

6

Per my understanding, you just want to loop your array to get each item's name, mail and some other fields. As you mentioned in your question, you can use the json body as input for the For Each loop. It's ok, ther is not need to to anything more. Please refer to the screenshot below:

  1. Initialize a variable like your json data. enter image description here

  2. Then parse it by "Parse JSON" action. enter image description here

  3. Now, set the body as input for the For each loop, and then use a variable and set the value with "mail" from "Parse JSON". enter image description here

  4. After running the logic app, we can see the mail field is also looped. You can use the "mail", "name" and other fields easily in your "For each". enter image description here enter image description here

Update:

I checked your json schema, but it seems can't match the json data you provided in your screenshot. May I know how did you generate your json schema, in my side I generate the json schema just by clicking the "Use sample payload to generate schema" button and it will generate the schema automatically. enter image description here

I use a json data sample with the same structure of yours' and generate its schema, please refer to the json data and schema below:

json data:

{
    "body": [
        {
            "@odata.type": "test",
            "id": "123456",
            "givenName": "test",
            "username": "test",
            "userPrincipalName": "test",
            "mail": "test@mail.com",
            "onPremisesSamAccountName": "test",
            "employeeId": "test",
            "createdDateTime": "testdate"
        },
        {
            "@odata.type": "test",
            "id": "123456",
            "givenName": "test",
            "username": "test",
            "userPrincipalName": "test",
            "mail": "test@mail.com",
            "onPremisesSamAccountName": "test",
            "employeeId": "test",
            "createdDateTime": "testdate"
        }
    ]
}

schema:

{
    "type": "object",
    "properties": {
        "body": {
            "type": "array",
            "items": {
                "type": "object",
                "properties": {
                    "@@odata.type": {
                        "type": "string"
                    },
                    "id": {
                        "type": "string"
                    },
                    "givenName": {
                        "type": "string"
                    },
                    "username": {
                        "type": "string"
                    },
                    "userPrincipalName": {
                        "type": "string"
                    },
                    "mail": {
                        "type": "string"
                    },
                    "onPremisesSamAccountName": {
                        "type": "string"
                    },
                    "employeeId": {
                        "type": "string"
                    },
                    "createdDateTime": {
                        "type": "string"
                    }
                },
                "required": [
                    "@@odata.type",
                    "id",
                    "givenName",
                    "username",
                    "userPrincipalName",
                    "mail",
                    "onPremisesSamAccountName",
                    "employeeId",
                    "createdDateTime"
                ]
            }
        }
    }
}
Hury Shen
  • 14,948
  • 1
  • 9
  • 18
  • Hi, thanks for your response! All that seemed pretty clear to me (it's not the first loop through a JSON collection I've built) but for some reason this one won't work. I think something's wrong with the JSON schema. I have pasted my entire schema in my original question. – Martijn Balink Dec 16 '19 at 15:36
  • Hi @MartijnBalink, it seems your json schema don't match your json data. I have updated my answer, please check it. If still have any problem, please feel free to let me know~ – Hury Shen Dec 17 '19 at 01:30
  • Hi @MartijnBalink, may I know if your problem was solved ? – Hury Shen Dec 20 '19 at 02:16
  • Hi @HuryShen , Could you please help with my question? – Lynn Aug 11 '20 at 21:51
  • 1
    Brilliant, best tutorial ever. Worked perfectly the first time through, thanks! – spadelives Dec 05 '20 at 00:17