Loop operation with JSON

Hi everyone,

I have a doubt regarding the iteration.

I created a definition json tiddler, it has this content:

{
    "name": "def",
    "items": {
        "case1": "option1",
        "case2": "option2",
        "case3": "option3"
    }
}

And an another tiddler, with this hardcode values, and i can see the result as a dropdown.

<$set name="jsondata" value={{definitionjson}}>
    <$select field='mystatus' class='dropdown'>
        <option>{{{[<jsondata>jsonget[items],[case1]]}}}</option>  
        <option>{{{[<jsondata>jsonget[items],[case2]]}}}</option>
        <option>{{{[<jsondata>jsonget[items],[case3]]}}}</option>
    </$select>
<$set>

But, i want to do it via list iteration,
I tried to use this, but it didnt work.

<select>
    <$list filter="[<jsondata>jsonget[items]keys[]]">
        <option><$view field="(key)" template="[<jsondata>jsonget[items][$index]keys[]]"/></option>
    </$list>
</select>

How can i solve this? Can anyone guide me?

Thanks

Try this: code is tested

<select>
    <$list filter="[{definitionjson}jsonget[items]]">
        <option><<currentTiddler>></option>
    </$list>
</select>

[{definitionjson} ... directly reads the content of the tiddler
... jsonget[items]] shows the options. So you do not need to mess with the keys if you do not want to.

If you do not want to overwrite currentTiddler in your list-widget you can use variable="item":

<select>
    <$list filter="[{definitionjson}jsonget[items]]" variable="item">
        <option><<item>></option>
    </$list>
</select>

hope that helps
-m

1 Like

Thank you very much.It worked for me