You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi,
I noticed the flatten-objects function here doesnt flatten nested arrays. I understand that flattening can have different meaning & expectations depending on each use case but my expectation when I want to flatten a json that it should cover everything. Of course considering arrays can get very complex given what arrays can hold. Part of learning jslt I wanted to take on this challenge to see if it can be done. I hope I was successful but part of doing this I found myself using some other helpful functions (see comments on each function) that I thought it might be nice to have not just for flattening but for other things and maybe have them as built function. Without further due , here is the code:
// This is different way of zipping where the key is the index itself. I found that provide more direct way if you need to access the index
// vs storing the index in another key.
def zip-by-index(json)
let res = if(is-array($json)) [for(zip-with-index($json)) {string(.index):.value}] else []
$res
//This is to make an array as complex object to create uniformity on how complex types in json can be processed. Im not sure there are
// many use cases for this but its nice to have as an option
def objectify-array(array)
let res = if($array==[] or $array==null) {}
else if(size($array)==1) $array[0]
else $array[-1]+objectify-array($array[0 : -1])
$res
def flatten_json(prefix,json)
let simple= {for($json) $prefix+.key:.value if(.key!=null and not(is-object(.value)) and not(is-array(.value)))}
let complex= [for($json) flatten_json($prefix+.key+".",.value) if(is-object(.value))]
let array= [for($json) flatten_json($prefix+.key+".",objectify-array(zip-by-index(.value))) if(is-array(.value))]
objectify-array($array)+objectify-array($complex)+$simple
flatten_json("",.)
Hi,
I noticed the flatten-objects function here doesnt flatten nested arrays. I understand that flattening can have different meaning & expectations depending on each use case but my expectation when I want to flatten a json that it should cover everything. Of course considering arrays can get very complex given what arrays can hold. Part of learning jslt I wanted to take on this challenge to see if it can be done. I hope I was successful but part of doing this I found myself using some other helpful functions (see comments on each function) that I thought it might be nice to have not just for flattening but for other things and maybe have them as built function. Without further due , here is the code:
Example:
Output:
I appreciate any well explained feedback.
Thanks
S
The text was updated successfully, but these errors were encountered: