> For the complete documentation index, see [llms.txt](https://docs.rewst.help/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.rewst.help/documentation/jinja/use-cases-and-best-practices/efficiently-extracting-nested-data.md).

# Efficiently extract nested data

Streamlining Data Retrieval with Jinja Filters vs. List Comprehension

Explore how to use `jsonpath_query`, list comprehension, and the `map` filter in Rewst for extracting data from JSON structures. Select the method that best suits your task's complexity and your familiarity with programming concepts.

## Example scenario

Extract event names from a JSON list containing detailed event information.

#### JSON structure:

```json
{
  "events": [
    {"details": {"name": "Event One", "location": "City A"}},
    {"details": {"name": "Event Two", "location": "City B"}}
  ]
}
```

### Method one: Use list comprehension

List comprehension is a straightforward Pythonic way to create lists by iterating over sequences.

**Example:**

```django
{{ [item.details.name for item in CTX.events] }}
```

**Explanation:**

This method loops through each event in `CTX.events`, extracting the `name` from the `details`.

1. **List Comprehension Syntax**: `[expression for item in list]` is standard list comprehension syntax.
2. **Iterating Over the `CTX.events`**: The `for item in CTX.events` part loops through each event in the `CTX.events` array.
3. **Extracting the `name`**: The `item.details.name` extracts the `name` property from the `details` object of each event.

### Method two: Use `jsonpath_query`

`jsonpath_query` provides a more direct approach to targeting and extracting nested data.

**Example:**

```django
{{ CTX.events | jsonpath_query('$[*].details.name') }}
```

**Explanation:**

This expression directly navigates to and extracts the `name` field from each event's `details`.

1. **Using the `jsonpath_query` Filter**: `jsonpath_query` is a filter applied to the `CTX.events` variable.
2. **Navigating the JSON Path**: `'$[*].details.name'` is the path expression. `$` denotes the root of the JSON structure, `[*]` accesses all elements in the `events` array, and `.details.name` navigates to the `name` within each `details` object.

### Method three: Use `map` with an attribute

**Example:**

```django
{{ CTX.events | map(attribute='details.name') }}
```

**Explanation**:

This example demonstrates using the `map` filter in Jinja to extract a specific attribute from each element in a sequence.

* **Apply the `map` filter**: The `map` filter is applied to the `CTX.events` variable. This Jinja2 filter iterates over each element in the given sequence.
* **Specify the attribute to extract**: The part `attribute='details.name'` tells the `map` filter to extract the `name` attribute from the `details` object within each element of `CTX.events`.

### Comparison and effectiveness

* **List comprehension**: Offers control and flexibility, best for simpler structures or specific manipulations.
* **Filters (`jsonpath_query`, `map`)**: Streamline the process with specialized functionality. Choose `jsonpath_query` for deep JSON navigation and `map` for straightforward, uniform transformations.

### Expected output

All methods output a list of event names:

```json
["Event One", "Event Two"]
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.rewst.help/documentation/jinja/use-cases-and-best-practices/efficiently-extracting-nested-data.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
