> ## Documentation Index
> Fetch the complete documentation index at: https://genai.codewithsiva.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# 5. JSON Output Parser

> Generate structured JSON output as standard Python dictionaries, with or without validation schemas

In this section, you will learn how to extract structured data in JSON format as standard Python dictionaries, with or without schemas.

## Objectives

1. Configure `JsonOutputParser` to generate arbitrary, freeform JSON structures.
2. Pair `JsonOutputParser` with Pydantic classes to output dictionary formats conforming to a strict schema.
3. Access parsed dictionary items directly in Python.

***

## Code Implementation

Each step of the implementation is preceded by extensive comments explaining the code logic.

### Method 1: Without a Schema (Freeform JSON)

Generate structured JSON outputs dynamically without defining Pydantic schemas beforehand.

```python theme={null}
from dotenv import load_dotenv
from langchain.prompts import ChatPromptTemplate
from langchain_core.output_parsers import JsonOutputParser
from langchain.chat_models import init_chat_model

load_dotenv()

# Initialize the Groq model using core abstractions
model = init_chat_model("llama-3.3-70b-versatile", model_provider="groq")

# Instantiate freeform json parser
json_parser = JsonOutputParser()

# Retrieve formatting instructions
format_instructions = json_parser.get_format_instructions()

# Create prompt
prompt_free = ChatPromptTemplate.from_messages([
    ("system", "You are an assistant that outputs structured data in JSON. {format_instructions}"),
    ("human", "List the top 3 cities in France and include their population and a famous landmark.")
])

# 1. Format prompt
messages_free = prompt_free.format_messages(format_instructions=format_instructions)

# 2. Invoke model
response_free = model.invoke(messages_free)

# 3. Parse response content
result_free = json_parser.parse(response_free.content)
print(result_free)
```

***

### Method 2: With Pydantic Schema (Structured Dict Output)

Enforce structured outputs while returning standard Python dictionaries instead of Pydantic object instances.

```python theme={null}
from pydantic import BaseModel, Field

# Define target model using Pydantic
class MovieInfo(BaseModel):
    title: str = Field(description="The name of the movie")
    director: str = Field(description="The director of the movie")
    release_year: int = Field(description="The year the movie was released")
    genres: list[str] = Field(description="Genres of the movie")

# Instantiate the parser with the schema
schema_parser = JsonOutputParser(pydantic_object=MovieInfo)
schema_instructions = schema_parser.get_format_instructions()

# Create prompt
prompt_schema = ChatPromptTemplate.from_messages([
    ("system", "Generate the details about the movie. {format_instructions}"),
    ("human", "Provide details for the movie 'Inception'.")
])

# 1. Format prompt
messages_schema = prompt_schema.format_messages(format_instructions=schema_instructions)

# 2. Invoke model
response_schema = model.invoke(messages_schema)

# 3. Parse response content
result_schema = schema_parser.parse(response_schema.content)
print(result_schema)
```

***

## Practice & Exercises

To reinforce what you've learned in this section, practice with the interactive notebook:

<CardGroup cols={1}>
  <Card title="Practice & Exercises" icon="laptop-code">
    Practice extracting JSON data from LLMs as standard Python dictionaries, with and without validation schemas.

    [💻 VS Code](vscode://file/Users/sivaprasad/Downloads/GenAI%20With%20Python/public/notebooks/langchain/5_parsers_json.ipynb) | [🚀 Colab](https://colab.research.google.com/github/prasad230776/genai-course/blob/master/public/notebooks/langchain/5_parsers_json.ipynb) | <a href="/public/notebooks/langchain/5_parsers_json.ipynb" download>📥 Download</a>
  </Card>
</CardGroup>
