Search code examples
jsonnode.jsswig-template

access json data using swig-template


{
"_id": "1",
"style": "13123",
"category": "dress",
"colors": {
    "Black": {
        "prestock": 50,
        "instock": 60,
        "inactive": 0
    },
    "Blue": {
        "prestock": 30,
        "instock": 0,
        "inactive": 0
    },
    "Red": {
        "prestock": 10,
        "instock": 60,
        "inactive": 0
    }
  }
}

i'm using swig-template to access 'colors' object i need to express each color in this list-format:

  • Black
  • Blue
  • Red
  • how can i access this json?

    ps. i tried other ways but no luck, what i have is {{style_list.colors|sort}} which gives me like this:

    Black, Blue, Red
    

    Solution

  • Use the built-in JavaScript method Object.keys

    <ul>
    {% for color in Object.keys(colors) %}
        <li>{{ color }}</li>
    {% endfor %}
    </ul>