# JSON

Parsing data from a response

```go
import (
    "encoding/json"
    "strings"
    "fmt"
)
```

## Parsing Into Struct

```go
type App struct {
    Id string `json:"id"`
    Title string `json:"title"`
}

data := []byte(`
    {
        "id": "k34rAT4",
        "title": "My Awesome App"
    }
`)

var app App
err := json.Unmarshal(data, &app)
```

### Struct => Json

```
b, err := json.Marshal(m)
```

## Simpler Parsing Into Struct

will need to be same as JSON keys and vals(not case sensitive)

```go
type Tour struct {
    Name, Price string 
}
```

## Printing Byte Array

```go
fmt.print("%s", byt)
```

## Decoder

Grab json data from content using you struct

```go
func toursFromJson(content string) []Tour { //content is array of JSON
    tours := make([]Tour, 0, 20)

    decoder := json.NewDecoder(string.NewReader(content))
    _, err := decoder.Token() //throw away that first '[' 
    checkError(err)

    var tour Tour
    for decoder.More() { //More asks if there is another data entity and if true returns true and moves decoder there
        err := decoder.Decoder(&tour)//parse JSON and only get those values in your struct leaving it in tour
        checkError(err)
        tours = append(tours, tour)
    }

    return tours
}
```


---

# 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://openai.gitbook.io/code-cheatsheets/all/go/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.
