[Feature]: Add "values" parameter to json.toList()
Opened this issue · 6 comments
Describe the Problem
We can currently only create a list of keys from a json object, not a list of the values using json.toList. It would be nice to be able to get all of the values in a list without having to use a loop in macroscript.
The Solution you'd like
Add a "values" parameter to the json.toList function.
Alternatives that you've considered.
No response
Additional Context
No response
Will [r: json.path.read(json, '*.$')] do what you need?
Not sure... I'm not too familiar with using the path functions and got an error with that.
The use case was with an object of class names (70+ classes) and levels where I wanted to quickly calculate the total levels. And while I can loop through all the keys to get a sum, it would be faster if I could have turned the values into a list and used add(). I was surprised I couldn't do it with json.toList() (I could only make a list of the keys).
Small version of the object:
{"Barbarian":0,"Bard":0,"Cleric":0,"Druid":0,"Fighter":0,"Monk":0,"Paladin":0,"Ranger":4,"Rogue":0,"Sorcerer":5,"Wizard":0}
This works for me:
[h: vJson = '{"Barbarian":0,"Bard":0,"Cleric":0,"Druid":0,"Fighter":0,"Monk":0,"Paladin":0,"Ranger":4,"Rogue":0,"Sorcerer":5,"Wizard":0}']
<br>ArrayOfValues: [r: json.path.read(vJson, "*")]
<br>Sum: [r: math.arraySum(json.path.read(vJson, "*"))]
This works for me:
[h: vJson = '{"Barbarian":0,"Bard":0,"Cleric":0,"Druid":0,"Fighter":0,"Monk":0,"Paladin":0,"Ranger":4,"Rogue":0,"Sorcerer":5,"Wizard":0}'] <br>ArrayOfValues: [r: json.path.read(vJson, "*")] <br>Sum: [r: math.arraySum(json.path.read(vJson, "*"))]
Yes, that does. Thanks. I'll make a nice little udf with that.
I'm guessing that's better than doing a loop through all the values... is it better than what having a "values" parameter in json.toList() would be?
At any rate, I still think it would be nice to have a "values" parameter in json.toList() for added simplicity and utility.
Not sure... I'm not too familiar with using the path functions and got an error with that.
Doh, I always forget the backslash befor the $
[r: json.path.read(json, '*.$')]
Doh, I always forget the backslash befor the
$[r: json.path.read(json, '*.$')]
I think github comments eat backslashes. ;)