Title: Accessing Inner Most Set within Json and Jinja2 with Flask
Tags: python;json;flask;jinja2
Question: Working with an API that returns me JSON, I can not figure out how to access the inner most element of the value. What I am left with is a single key and value which is all the data.

```{
  "results": [
    {
      "DataverseName": "Metadata",
      "DatatypeName": "NodeGroupRecordType",
      "Derived": {
        "Tag": "RECORD",
        "IsAnonymous": false,
        "EnumValues": null,
        "Record": {
          "IsOpen": true,
          "Fields": {
            orderedlist: [
              {
                "FieldName": "GroupName",
                "FieldType": "string"
              },
              {
                "FieldName": "NodeNames",
                "FieldType": "Field_NodeNames_in_NodeGroupRecordType"
              },
              {
                "FieldName": "Timestamp",
                "FieldType": "string"
              }
            ]
          }
        },
        "Union": null,
        "UnorderedList": null,
        "OrderedList": null
      },
      "Timestamp": "Wed Apr 30 15:55:24 PDT 2014"
    }
  ]
}
```

I am attempting to access the set ```orderedlist``` and need to grab the value of ```FieldName```. In python, I am using SimpleJson and when iterating over result, everything is mixed together. Would I have to re-encode the JSON such that values are now keys, and continue till I reach ```ordredlist```?

Python:

```j = json.loads(response.text)
return render_template('fields.html', response=j, dataverse=dataset)
```

HTML / JinJa2

```{% for key, value in response.items() %}
    {% for items in value %}
        {{ items }}
    {% endfor %}
{% endfor %}
```

Comment: Is the first chunk of code above the actual json that is returned?


Here is another answer: As your question is related to accessing inner data structure from Jinja2 template, I simplified my sample (ignoring Flask stuff). Applying my answer to Flask will be trivial.

First, your JSON data are invalid, I had to correct ```orderedlist``` enclosing it in ```"``` and I set the ```l``` to uppercase, ending ```"orderedList"```.

Then, accessing data from Jinja2 is fun - you may access it using dot notation (but can also use ["SomeName"]. Be aware, that Jinja2 will hide many problems, if data you ask for are not existing. You shall play with your template step by step from outer structures deeper and you will get what you want.

```from jinja2 import Template
import json

response_text = """
{
  "results": [
    {
      "DataverseName": "Metadata",
      "DatatypeName": "NodeGroupRecordType",
      "Derived": {
        "Tag": "RECORD",
        "IsAnonymous": false,
        "EnumValues": null,
        "Record": {
          "IsOpen": true,
          "Fields": {
            "orderedList": [
              {
                "FieldName": "GroupName",
                "FieldType": "string"
              },
              {
                "FieldName": "NodeNames",
                "FieldType": "Field_NodeNames_in_NodeGroupRecordType"
              },
              {
                "FieldName": "Timestamp",
                "FieldType": "string"
              }
            ]
          }
        },
        "Union": null,
        "UnorderedList": null,
        "OrderedList": null
      },
      "Timestamp": "Wed Apr 30 15:55:24 PDT 2014"
    }
  ]
}
"""

data = json.loads(response_text)

templ_str = """
{% for rec in data.results %}
    Results: -------------
    {% for fldinfo in rec.Derived.Record.Fields.orderedList %}
        Field: Name: {{fldinfo.FieldName}} Type: {{fldinfo.FieldType}}
    {% endfor %}
{% endfor %}
"""

template = Template(templ_str)
print template.render(data=data)
```

Running it we get:

```Results: -------------

    Field: Name: GroupName Type: string

    Field: Name: NodeNames Type: Field_NodeNames_in_NodeGroupRecordType

    Field: Name: Timestamp Type: string
```


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 1, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2014:00:05/27/14', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<python><json><flask><jinja2>', 'Title': 'Accessing Inner Most Set within Json and Jinja2 with Flask', 'ViewCount': 1312, 'id': 23897146, 'user_AboutMe': '<p>Long time reader of the stack overflow family.</p>\n', 'user_AccountId': 1977421.0, 'user_DisplayName': 'Adam', 'user_Reputation': 510.0, 'user_Views': 67.0, 'user_id': 1774903.0}}