Parsing Users for Names and IDs

Anyone have an ideas on good ways to parse the results from a Users Get request? Having a hard time creating the Custom Connector in Power Automate with this.

{
  "users": [
    [
    123123,
    "John Smith"
    ],
    [
    123124,
    "Jane Smith"
    ]
  ]
}

Should look like:

{
  "users": [
    [
    "id": 123123,
    "name": "John Smith"
    ],
    [
    "id": 123124,
    "name": "Jane Smith"
    ]
  ]
}

we don’t use power animater but i think you can use inline javascript.

Made a Example Code:

// Example Data
let input = {
  "users": [
    [
    123123,
    "John Smith"
    ],
    [
    123124,
    "Jane Smith"
    ]
  ]
}

// Emty Object
let result = {"users": []}

// Runs over each users entry
input.users.forEach((user) =>{

  // New Layout of the user Object
  let templateUser = {
    "user": user[0],
    "name": user[1]
  } 
  // Add each new User Array into result.users
  result.users.push(templateUser);
});

// Output
console.log(result);

In Power Automate (previously Microsoft Flow) you cannot use inline javascript directly - there is some limited Javascript-like coding, but it’s not very useful here.

Logic Apps and I believe Power Apps (also based on the Power Platform) can run JavaScript I believe.

However, not all is lost, with some creative JSON Scheme-a-ing and some data operations, I could get Automate so that it would parse out the User’s ID and Display Name.
The whole process is explained below, and it’s a bit of a doozy, but the code is provided

NOTE: This is highly dependent on Syncro not changing the way that this returns, and currently there is not a defined Schema for this method for us to trust it will return as. But if we ASSUME that the first item inside of a user is always the UserID and the Second item is always their DisplayName, we can just parse it through as such

First - I parse a response from the Syncro API with the first JSON Schema I made

This will

  • validate the JSON Matches the schema (to make certain you don’t treat uncorrected JSON like corrected ones)
  • recognize the Body (the entire value input/validated)
  • Recognize “AllUsers” (the array of all unmodified user objects)
  • Recognize "UserFields (Each array of items within each item in “AllUsers”)

From that, we take the AllUsers array of user objects and map it to Property Names in an object.
Each input looks like this

[
    134465,
    "John Hancock"
  ]

Now, unfortunately I couldn’t see an easy to use the UserFields I made before, because it would iterate through each field for each user and it was too deep.
So we have to use a function to recognize this object INSIDE of the current item as an array, and then get the First or Last Item
The mapping looks like this
image
and those expressions are first(array(item())) and last(array(item())) Respectively.

Functionally, this takes the Object from being an

- Array of (
    - Arrays of (
        - both of (
            - integers and 
            - strings
        )
    )
)

to

- Array of (
    - Objects with(
        - Property 'UserID" - Type Integer
        - Property 'Displayname  - Type String
        )
    )
)

that change looks like this

NOTE: This is the part that - based on current API Docs - may not be relied upon to always be the same order or could be changed later - You could add some additional error-checking and make sure the first item was an integer, etc - but that seems like unneeded extra work until it becomes a problem

Next, we have Values, but they are not parsed into nice-to use ‘dynamic content’.
Without the dynamic Content - we would have to make references like body('ParseJSON-ParseProperties')['Users']?['DisplayName'] - and it would not be able to handle any action names being changed

So we run it through a new “Parse JSON” action with a slightly difference Schema to pull out the helpful items in the JSON

This takes the ‘Dynamic Content’ options from this
image
to this
image
Note: The schema is constructed in such a way that if you tried to pass an unmodified Syncro /Users result to the later Schema - it will error, and if you try to pass the modified JSON to the earlier Schema, it will also error. This is intentional so that you can’t accidentally try to use a modified json as an unmodified one and vice-versa.

From here, you are good to put any output data you want, or take actions, or whatever you would like.
You could make a text body to output to an email
image
image

Or Select a certain user to get their ID number and further query something else


image

I will get a link to Example code and/or github and/or deploy button here in a little bit
Exporting/Importing flows is always this annoying ZIP format that is odd but you should be able to go to Flow > Manage > Import and import the ZIP without much trouble

2 Likes

This is beautiful! Thank you!

I managed to get what I needed. I replicate what you posted but forgot that I had this post. That said, I learned a few things reading your answer, so thanks again!

I needed to check if a Technician was already created. If not, then send a note to myself to create it in Syncro (as creating a Technician is not publicly documented API).

In the below image, I scrapped the list, matched the name to a SQL table. The key was learning item()?[1], referencing each item then taking the 2nd value.

Since I don’t see a way to upload an attachment like a zip, here is a link to my sample flow and data ZIP
since I typed all this up, I’m going to try and throw it on a blog for next time I end up wanting to do this
it will be somewhere on Systems Management Squad (sysmansquad.com)

This is the link to the file from the PR still in progress
BlogFiles/ParseSyncroUserAPIResponseExample_20211124211328.zip at PR · SysManSquad/BlogFiles (github.com)

but it’ll get merged later to a different place (though this won’t work yet
BlogFiles/ParseSyncroUserAPIResponseExample.zip at master

Interesting though - I could swear I was trying that same item()?[1] and item()?[0] and it was just telling me it didn’t have that field
If that part is working - them you may even be able to replace that Compose with a ParseJSON like this so you can get the easier to follow and work with “Dynamic Expressions” - and not all the annoying “Outputs” references everywhere >.>
Those JSON Schema’s are just “Generate from Sample”

image

But glad you got it going!