JSONExplore ( json ; endpoint )
This is a helper function that I use all the time to help me explore and navigate unfamiliar JSON objects in the data viewer such as exploring returned api results. The keys at the given endpoint are listed at the top. The formatted structure is listed next, and then the raw structure is listed last.
Average rating: 5.0 (1 vote) Log in to vote
Robert D Trammel - Show more from this author
https://www.linkedin.com/in/robertdtrammel/ |
name
uid
==== Structure ====
{
"name" : "Beyonce Knowles"
"uid" : "1"
}
==== Data ====
"" = {"name":"Beyonce Knowles","uid": "1"}
Function definition: (Copy & paste into FileMaker's Edit Custom Function window)
If you pass in a blank string for the second parameter, it will get the keys at the top level and display the whole object. The idea here is that for each endpoint you want to step into you would just add a period and then type the next key in the key list that you want to explore
For Example:
------
1st iteration
------
JSONExplore ( data ; "" )
==== Keys ====
users
...
------
2nd iteration
------
JSONExplore ( data ; "users" )
==== Keys ====
0
1
2
3
...
------
3rd iteration
------
JSONExplore ( data ; "users[0]" )
==== Keys ====
id
name
age
...
-------
From there you can set up your script or while loop to loop through the array and easily process the data into your solution.
For example you might do:
$keys = JSONListKeys ( data ; "users" );
$keyLn = ValueCount ( $keys );
Loop
Exit Loop If [ Let ( $I = $I + 1 ; $I > $keyLn )
Set Variable [ $endpoint ; "users[" & GetValue ( $keys ; $I ) & "]" ]
Set Variable [ $name ; JSONGetElement ( data ; $endpoint & ".name" ) ]
Set Variable [ $id ; JSONGetElement ( data ; $endpoint & ".id" ) ]
End Loop
Comments
Robert D Trammel Dec 7, 2021 |
||
Note to other developers, I tried to make it so that once you reach your final endpoint, the output is the exact calculation you would need to copy/paste into whatever process you were writing. So for instance, once you land on users[0].name, instead of seeing the entire keys/formatted/raw output, you would see JSONGetElement ( data ; "users[0].name" ) and then its output. This worked well when the input was a field because I could just do GetFieldName. However, when the input was a variable, there was no apparent way to do like a GetVariableName function. As a result, the output would just break, so I cleaned that out entirely but I'm still holding out hope that someone here either has, or will figure it out. Please keep me posted if that happens. | ||
Note: these functions are not guaranteed or supported by BrianDunning.com. Please contact the individual developer with any questions or problems.