whoAmi21
whoAmi21

Reputation: 105

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
    

    Upvotes: 1

    Views: 1500

    Answers (1)

    Paul Armstrong
    Paul Armstrong

    Reputation: 7156

    Use the built-in JavaScript method Object.keys

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

    Upvotes: 2

    Related Questions