maptool icon indicating copy to clipboard operation
maptool copied to clipboard

[Feature]: Add "values" parameter to json.toList()

Open FullBleed opened this issue 4 months ago • 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

FullBleed avatar Aug 30 '25 11:08 FullBleed

Will [r: json.path.read(json, '*.$')] do what you need?

cwisniew avatar Aug 30 '25 13:08 cwisniew

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}

FullBleed avatar Aug 30 '25 15:08 FullBleed

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, "*"))]

Baaaaaz avatar Aug 31 '25 04:08 Baaaaaz

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.

FullBleed avatar Aug 31 '25 05:08 FullBleed

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, '*.$')]

cwisniew avatar Aug 31 '25 09:08 cwisniew

Doh, I always forget the backslash befor the $

[r: json.path.read(json, '*.$')]

I think github comments eat backslashes. ;)

FullBleed avatar Aug 31 '25 19:08 FullBleed