Jsonnet: iterating through objects as key value pairs (cf. pythons .items())

Created on 17 Jul 2018  路  7Comments  路  Source: google/jsonnet

sometimes it's useful to process objects by iterating through the key value pairs

python has

obj = {"a": 1, "b": 2}
{k:v*2 for k,v in obj.items()}

jq has

echo '{"a": 1, "b": 2}'|jq '[to_entries[]|{key: .key, value: (.value * 2)}]|from_entries'

is there something similar in jsonnet? it seems like the list and object comprehensions could return (k,v) tuples if the right hand side is an object

enhancement stdlib

Most helpful comment

The issue is about matching the python feature of for k, v in obj, which I think would be useful even though a simple work around is to loop over the fields doing obj[k] each time. It is indeed not implemented yet.

All 7 comments

You can achieve that using std.objectFields or std.objectFieldsAll to get list of keys (depending on whether you want to include hidden fields). If you need both keys and values, there's no builtin function, but it's pretty simple to write yourself.

local keysAndValues(obj) = std.map(function(x) [x, obj[x]], objectFields(obj))

(note: I haven't tested it yet)

What you describe could be a nice shortcut.

@sparkprime Perhaps we should add something like keysAndValues to stdlib

Though it wouldn't matter for the usual small objects, implementing this as a native function would save the lookup for each key to retrieve its corresponding value (obj[x] above).

You can do {k: obj[k]*2 for k in std.objectFields(obj)}

Iteration over objects would be nice too {k: e for k, v in ...}
And it's just syntax sugar to implement it.

@sparkprime The syntax sugar that you gave as an example is not implemented yet, right? It throws the following exception: object comprehensions can only have [e] fields.

It looks like the syntax is as follows:{ [key]: last_values[key] for key in std.objectFields(last_values) by looking at std.jsonnet. It would be great if we can add it to the documentation.

P.S: Given that we have this syntax, why don't we close the issue? There are more than 100 open issues at the moment. 馃槵

The issue is about matching the python feature of for k, v in obj, which I think would be useful even though a simple work around is to loop over the fields doing obj[k] each time. It is indeed not implemented yet.

I also noticed the lack of such a function, and I was surprised that std.mapWithKey didn't generate an array. I opened https://stackoverflow.com/questions/63915576/jsonnet-std-mapwithkey-not-generating-an-array/63915745#63915745 and answered my self based upon @sbarzowski's response.

Also, I noticed there's a lack of std.objectValues which is a bit annoying as well, but once again, it can be worked around if using a std.map and std.objetFields.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mbrukman picture mbrukman  路  5Comments

sbarzowski picture sbarzowski  路  7Comments

davidzchen picture davidzchen  路  6Comments

ant31 picture ant31  路  6Comments

nikolay picture nikolay  路  7Comments