# Create, Collect into Object

This is used to construct objects (or maps). This can be used against existing yaml, or to create fresh yaml documents.

## Collect empty object

Running

```bash
yq --null-input '{}'
```

will output

```yaml
{}
```

## Wrap (prefix) existing object

Given a sample.yml file of:

```yaml
name: Mike
```

then

```bash
yq '{"wrap": .}' sample.yml
```

will output

```yaml
wrap:
  name: Mike
```

## Using splat to create multiple objects

Given a sample.yml file of:

```yaml
name: Mike
pets:
  - cat
  - dog
```

then

```bash
yq '{.name: .pets.[]}' sample.yml
```

will output

```yaml
Mike: cat
Mike: dog
```

## Working with multiple documents

Given a sample.yml file of:

```yaml
name: Mike
pets:
  - cat
  - dog
---
name: Rosey
pets:
  - monkey
  - sheep
```

then

```bash
yq '{.name: .pets.[]}' sample.yml
```

will output

```yaml
Mike: cat
Mike: dog
---
Rosey: monkey
Rosey: sheep
```

## Creating yaml from scratch

Running

```bash
yq --null-input '{"wrap": "frog"}'
```

will output

```yaml
wrap: frog
```

## Creating yaml from scratch with multiple objects

Running

```bash
yq --null-input '(.a.b = "foo") | (.d.e = "bar")'
```

will output

```yaml
a:
  b: foo
d:
  e: bar
```


---

# 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://mikefarah.gitbook.io/yq/operators/create-collect-into-object.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.
