jsonListAllKeys ( _json ; _key )
Get a list of all keys under the argument key from json.
Be the first to rate this function Log in to vote
Teruhiro Komaki - Show more from this author
frudens Inc. https://frudens.jp |
JSONSetElement ( "" ;
[ "id" ; 12345 ; JSONNumber ] ;
[ "user.name" ; "test" ; JSONString ] ;
[ "user.age" ; 30 ; JSONNumber ] ;
[ "like[0]" ; JSONSetElement ( "" ; "fruits" ; "Apple, Banana" ; JSONString ) ; JSONObject ] ;
[ "like[1]" ; JSONSetElement ( "" ; "meals" ; "ramen, soba" ; JSONString ) ; JSONObject ]
) ; "" )
.like
.like[0]
.like[0].fruits
.like[1]
.like[1].meals
.user
.user.age
.user.name
Function definition: (Copy & paste into FileMaker's Edit Custom Function window)
# History
20210326 | Fixed it to a built-in function instead of a custom function.
```
// If the value (~value) retrieved with findKey is json, it is recursive.
~value = JSONGetElement ( ~json ; ~findKey ) ;
~childKeys = If ( isJson ( ~value ) ; frd_jsonListAllKeys ( ~json ; ~findKey ) ) ;
↓
// If the value (~value) retrieved with findKey is json, it is recursive.
~isJson = Left ( JSONFormatElements ( JSONGetElement ( ~json ; ~findKey ) ) ; 1 ) <> "?" ;
~childKeys = If ( ~isJson ; frd_jsonListAllKeys ( ~json ; ~findKey ) ) ;
```
Get a list of all keys under the argument key from json.
# Sampl JSON
JSONSetElement ( "" ;
[ "id" ; 12345 ; JSONNumber ] ;
[ "user.name" ; "test" ; JSONString ] ;
[ "user.age" ; 30 ; JSONNumber ] ;
[ "like[0]" ; JSONSetElement ( "" ; "fruits" ; "Apple, Banana" ; JSONString ) ; JSONObject ] ;
[ "like[1]" ; JSONSetElement ( "" ; "meals" ; "ramen, soba" ; JSONString ) ; JSONObject ]
)
{"id":12345,"like":[{"fruits":"Apple, Banana"},{"meals":"ramen, soba"}],"user":{"age":30,"name":"test"}}
# Example1
Input:
frd_jsonListAllKeys (
JSONSetElement ( "" ;
[ "id" ; 12345 ; JSONNumber ] ;
[ "user.name" ; "test" ; JSONString ] ;
[ "user.age" ; 30 ; JSONNumber ] ;
[ "like[0]" ; JSONSetElement ( "" ; "fruits" ; "Apple, Banana" ; JSONString ) ; JSONObject ] ;
[ "like[1]" ; JSONSetElement ( "" ; "meals" ; "ramen, soba" ; JSONString ) ; JSONObject ]
) ; "user" )
Output:
user.age
user.name
# Example2
Input:
frd_jsonListAllKeys (
JSONSetElement ( "" ;
[ "id" ; 12345 ; JSONNumber ] ;
[ "user.name" ; "test" ; JSONString ] ;
[ "user.age" ; 30 ; JSONNumber ] ;
[ "like[0]" ; JSONSetElement ( "" ; "fruits" ; "Apple, Banana" ; JSONString ) ; JSONObject ] ;
[ "like[1]" ; JSONSetElement ( "" ; "meals" ; "ramen, soba" ; JSONString ) ; JSONObject ]
) ; "like" )
Output:
like[0]
like[0].fruits
like[1]
like[1].meals
Comments
Joshua Willing, Willing Apps Mar 25, 2021 |
||
Hi, thanks for this great function. I noticed it contains a dependency on another CF `isJson`. I changed the code to this and it works without any dependencies: ``` While ( [ ~prevKey = _key ; ~json = _json ; ~keys = JSONListKeys ( ~json ; ~prevKey ) ; ~max = ValueCount ( ~keys ) ; ~i = 0 ; ~resultKeys = "" ] ; not ( ~i >= ~max ) ; [ ~i = ~i + 1 ; // If the argument key is an array, it should be [N]. ~key = GetValue ( ~keys ; ~i ) ; ~isNum = Exact ( ~key ; Filter ( ~key ; "0123456789" ) ) ; ~newKey = If ( ~isNum ; "[" & ~key & "]" ; ~key ) ; ~findKey = If ( ~isNum ; ~prevKey & ~newKey ; ~prevKey & "." & ~newKey ) ; ~resultKeys = List ( ~resultKeys ; ~findKey ) ; // If the value (~value) retrieved with findKey is json, it is recursive. ~isJson = Left ( JSONFormatElements ( JSONGetElement ( ~json ; ~findKey ) ) ; 1 ) <> "?" ; ~childKeys = If ( ~isJson ; frd_jsonListAllKeys ( ~json ; ~findKey ) ) ; ~resultKeys = List ( ~resultKeys ; ~childKeys ) ] ; ~resultKeys ) /*While*/ ``` |
||
Joshua Willing, Willing Apps Mar 25, 2021 |
||
Hi, thanks for this great function. I noticed it contains a dependency on another CF `isJson`. I changed the code to this and it works without any dependencies: ``` While ( [ ~prevKey = _key ; ~json = _json ; ~keys = JSONListKeys ( ~json ; ~prevKey ) ; ~max = ValueCount ( ~keys ) ; ~i = 0 ; ~resultKeys = "" ] ; not ( ~i >= ~max ) ; [ ~i = ~i + 1 ; // If the argument key is an array, it should be [N]. ~key = GetValue ( ~keys ; ~i ) ; ~isNum = Exact ( ~key ; Filter ( ~key ; "0123456789" ) ) ; ~newKey = If ( ~isNum ; "[" & ~key & "]" ; ~key ) ; ~findKey = If ( ~isNum ; ~prevKey & ~newKey ; ~prevKey & "." & ~newKey ) ; ~resultKeys = List ( ~resultKeys ; ~findKey ) ; // If the value (~value) retrieved with findKey is json, it is recursive. ~isJson = Left ( JSONFormatElements ( JSONGetElement ( ~json ; ~findKey ) ) ; 1 ) <> "?" ; ~childKeys = If ( ~isJson ; frd_jsonListAllKeys ( ~json ; ~findKey ) ) ; ~resultKeys = List ( ~resultKeys ; ~childKeys ) ] ; ~resultKeys ) /*While*/ ``` |
||
Teruhiro Komaki, frudens Inc. Mar 25, 2021 |
||
Dear Johnson Wang, Thank you for your comments. I have fixed it as you commented. |
||
Note: these functions are not guaranteed or supported by BrianDunning.com. Please contact the individual developer with any questions or problems.