Enumerate Json Properties Example

This example shows how to use the Json vipers GetByPosition and GetValueByPosition. Note the use of default in GetValueByPosition, because Object properties (nested Json values) do not translate to a native Sxml type.

GetByPosition returns a Json string that can be used to populate a new Json macro directly.

The last for loop demonstrates the two ways of getting a value by position: through the name, using GetNameByPosition, or directly from the Json, using GetValueByPosition

Smartsite SXML CopyCode image Copy Code
<se:json>
    {
        "first": "1st",
        "second": "IInd",
        "third": [{"test":"a"},{"test":"b"},{"test":"d"},{"test":"c"}],
        "fourth": {"first":1,"second":2,"third":3}
    }
</se:json>
<se:for from="1" to="{scope.json.length()}">
    {scope.json.getvaluebyposition(this.index()-1,default="{Json}")}
</se:for>
==
<se:for from="1" to="{scope.json.length(third)}">
    <se:json localid="nested">
        {scope.json.getbyposition("third",this.parent.index()-1)}
    </se:json>
    {this.index()}: {scope.nested.get(test)}
</se:for>
==
<se:for from="0" to="{sys.eval(scope.json.length(fourth)-1)}">
    {buffer.set(propname,scope.json.getnamebyposition(fourth,this.index()))}
    {this.index()}; {buffer.get(propname)}; {scope.json.getvalue("fourth." + $propname)}; {scope.json.getvaluebyposition(fourth, this.index())}
</se:for>
Example Result CopyCode image Copy Code
1st

    IInd

    {Json}

    {Json}

==

    
    1: "a"

    
    2: "b"

    
    3: "d"

    
    4: "c"

==

    
    0; first; 1; 1

    
    1; second; 2; 2

    
    2; third; 3; 3