Add new keys to a dictionary (in a loop) / converting array (list) to dictionary

Hello,
I’m new to shortcuts (but a mediom experienced programmer c/c++ javascript). Please be patient if I ask stupid questions.

I have a array of records (dictionaries) form an database query (text items and a random id)

[
  {"id":131,"text":"Text 1"},
  {"id":192,"text":"Text 2"},
  {"id":123,"text":"Text 3"}
]

No I like the user choose a text using Choose from List and process further with the id (not the text)

if my input to ’ Choose from List` looks like this it works as expected

{
  "Text 1":131,
  "Text 2":192,
  "Text 3":123
}

But how to convert the array into the dictionary?

I repeat with each item in the array to get all dictionaries in from the array
I’m stuck in setting a key in an dictionary by a variable using Set Dictionary Value It seems that it does not support new keys - please correct me if I’m wrong

Do anybody can help me here? How can I populate a dictionary with (new) keys defined by variables.
Every help appreciated.

Chris

PS: For people who a familiar with JavaScript the solution would look like this (perhaps this clarifies my problem)

var result = {};
var input = [
  {"id":131,"text":"Text 1"},
  {"id":192,"text":"Text 2"},
  {"id":123,"text":"Text 3"}
]
for (let item of input) {
  result[item.text] = item.id;
}
console.log(result);

I think you want something like this based on your description and JavaScript.

https://www.icloud.com/shortcuts/68597a3d8b1f4e82a9479fb150a1643d

1 Like

Great - the result is exactly what I was looking for!
Only had time to do a quick test. Will look into it in detail later.
Thank you very much!

(Funny that a few lines of code can explain a problem easier than many words)