# JSONObject
<span class="source-link">[[Source]](src/json/json_object.md#L-0-4)</span>

Immutable JSON object backed by a persistent hash map.

Construction is via chained `.update()` calls, each returning a new object
with structural sharing:

```pony
let obj = JSONObject
  .update("name", "Alice")
  .update("age", I64(30))
```


```pony
class val JSONObject
```

## Constructors

### create
<span class="source-link">[[Source]](src/json/json_object.md#L-0-20)</span>


```pony
new val create(
  data': Map[String val, JSONValue] = with structural sharing:

  ```pony
  let obj = JSONObject
    .update("name", "Alice")
    .update("age", I64(30))
  ```
  """

  let _data: pc.Map[String, JSONValue]

  new val create(
    data': pc.Map[String, JSONValue] = pc.Map[String, JSONValue)
: JSONObject val^
```
#### Parameters

*   data': [Map](collections-persistent-Map.md)\[[String](builtin-String.md) val, [JSONValue](json-JSONValue.md)\] = with structural sharing:

  ```pony
  let obj = JSONObject
    .update("name", "Alice")
    .update("age", I64(30))
  ```
  """

  let _data: pc.Map[String, JSONValue]

  new val create(
    data': pc.Map[String, JSONValue] = pc.Map[String, JSONValue

#### Returns

* [JSONObject](json-JSONObject.md) val^

---

## Public Functions

### apply
<span class="source-link">[[Source]](src/json/json_object.md#L-0-25)</span>


Look up a value by key. Raises if key is not present.


```pony
fun box apply(
  key: String val)
: JSONValue ?
```
#### Parameters

*   key: [String](builtin-String.md) val

#### Returns

* [JSONValue](json-JSONValue.md) ?

---

### get_or_else
<span class="source-link">[[Source]](src/json/json_object.md#L-0-31)</span>


Look up a value by key, returning default if absent.


```pony
fun box get_or_else(
  key: String val,
  default: JSONValue)
: JSONValue
```
#### Parameters

*   key: [String](builtin-String.md) val
*   default: [JSONValue](json-JSONValue.md)

#### Returns

* [JSONValue](json-JSONValue.md)

---

### contains
<span class="source-link">[[Source]](src/json/json_object.md#L-0-37)</span>


Check whether a key is present.


```pony
fun box contains(
  key: String val)
: Bool val
```
#### Parameters

*   key: [String](builtin-String.md) val

#### Returns

* [Bool](builtin-Bool.md) val

---

### size
<span class="source-link">[[Source]](src/json/json_object.md#L-0-43)</span>


Number of key-value pairs.


```pony
fun box size()
: USize val
```

#### Returns

* [USize](builtin-USize.md) val

---

### update
<span class="source-link">[[Source]](src/json/json_object.md#L-0-49)</span>


Return a new object with the key set to value.


```pony
fun box update(
  key: String val,
  value: JSONValue)
: JSONObject val
```
#### Parameters

*   key: [String](builtin-String.md) val
*   value: [JSONValue](json-JSONValue.md)

#### Returns

* [JSONObject](json-JSONObject.md) val

---

### remove
<span class="source-link">[[Source]](src/json/json_object.md#L-0-55)</span>


Return a new object without the given key. No-op if key is absent.


```pony
fun box remove(
  key: String val)
: JSONObject val
```
#### Parameters

*   key: [String](builtin-String.md) val

#### Returns

* [JSONObject](json-JSONObject.md) val

---

### keys
<span class="source-link">[[Source]](src/json/json_object.md#L-0-61)</span>


Iterate over keys.


```pony
fun box keys()
: MapKeys[String val, JSONValue, HashEq[String val] val] ref
```

#### Returns

* [MapKeys](collections-persistent-MapKeys.md)\[[String](builtin-String.md) val, [JSONValue](json-JSONValue.md), [HashEq](collections-HashEq.md)\[[String](builtin-String.md) val\] val\] ref

---

### values
<span class="source-link">[[Source]](src/json/json_object.md#L-0-67)</span>


Iterate over values.


```pony
fun box values()
: MapValues[String val, JSONValue, HashEq[String val] val] ref
```

#### Returns

* [MapValues](collections-persistent-MapValues.md)\[[String](builtin-String.md) val, [JSONValue](json-JSONValue.md), [HashEq](collections-HashEq.md)\[[String](builtin-String.md) val\] val\] ref

---

### pairs
<span class="source-link">[[Source]](src/json/json_object.md#L-0-73)</span>


Iterate over (key, value) pairs.


```pony
fun box pairs()
: MapPairs[String val, JSONValue, HashEq[String val] val] ref
```

#### Returns

* [MapPairs](collections-persistent-MapPairs.md)\[[String](builtin-String.md) val, [JSONValue](json-JSONValue.md), [HashEq](collections-HashEq.md)\[[String](builtin-String.md) val\] val\] ref

---

### print
<span class="source-link">[[Source]](src/json/json_object.md#L-0-79)</span>


Compact JSON serialization.


```pony
fun box print()
: String iso^
```

#### Returns

* [String](builtin-String.md) iso^

---

### pretty_print
<span class="source-link">[[Source]](src/json/json_object.md#L-0-85)</span>


Pretty-printed JSON serialization.


```pony
fun box pretty_print(
  indent: String val = "  ")
: String iso^
```
#### Parameters

*   indent: [String](builtin-String.md) val = "  "

#### Returns

* [String](builtin-String.md) iso^

---

