# JSON

**Content-Type: "application/json"**

The `Context.JSON(v, ...opts)` is the method which sends JSON responses to the client. It accepts the value and optional settings for rendering. The `JSON` options structure looks like this:

```go
// JSON contains the options for the JSON (Context's) Renderer.
type JSON struct {
    // http-specific
    StreamingJSON bool
    // content-specific
    UnescapeHTML bool
    Indent       string
    Prefix       string
    ASCII        bool
    // if true then it prepends a "while(1);" when Go slice (to JSON Array) value.
    Secure       bool
    // proto.Message specific marshal options.
    Proto ProtoMarshalOptions
}
```

> If `Indent` field is empty and the application runs without optimizations, the `Indent` field will be automatically set to `2 spaces`.

So, if we want to write a JSON with indentation of four spaces and prefixed with `while(1)` we can do something like that:

```go
func handler(ctx iris.Context) {
    response := []string{"val1", "val2", "val3"}
    options := iris.JSON{Indent: "    ", Secure: true}
    ctx.JSON(response, options)
}
```

**Result**

```json
while(1);[
    "val1",
    "val2",
    "val3"
]
```

As with all RESTful rich responses, any marshallable (JSON at this case) Go type can be given. If we want to render a Go struct as JSON, the struct's fields we want to render should be [**exported**](https://tour.golang.org/basics/3), and optionally tagged with the `json` struct tag. Look the exaple below:

```go
type User struct {
    Firstname    string `json:"firstname"`
    Lastname     string `json:"lastname"`
    IgnoredField int    `json:"-"`
}

func handler(ctx iris.Context) {
    response := User{
        Firstname: "makis",
        Lastname: "maropoulos",
        IgnoredField:42,
    }

    ctx.JSON(response)
}
```

**Result**

```json
{
  "firstname": "makis",
  "lastname": "maropoulos"
}
```

## ASCII

Use the `ASCII` field to generate ASCII-only JSON with escaped non-ASCII characters.

```go
app.Get("/json_ascii", func(ctx iris.Context) {
    response := iris.Map{"lang": "GO-虹膜", "tag": "<br>"}
    options := iris.JSON{Indent: "    ", ASCII: true}
    ctx.JSON(response, options)
})
```

**Result**

```json
{
    "lang": "GO-\u8679\u819c",
    "tag": "\u003cbr\u003e"
}
```

## UnescapeHTML

Do not replace special HTML characters with their unicode entities using the `UnescapeHTML` field.

```go
app.Get("/json_raw", func(ctx iris.Context) {
    options := iris.JSON{UnescapeHTML: true}
    ctx.JSON(iris.Map{
        "html": "<b>Hello, world!</b>",
    }, options)
})
```

**Result**

```json
{"html":"<b>Hello, world!</b>"}
```


---

# Agent Instructions: Querying This Documentation

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

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://iris-go.gitbook.io/iris/responses/json.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
