# JsonLens
<span class="source-link">[[Source]](src/json/json_lens.md#L-0-1)</span>

Composable, reusable JSON path for reading and modifying nested values.

Define a lens by chaining key/index steps, then apply it to any document:

```pony
let host_lens = JsonLens("config")("database")("host")

// Read
match host_lens.get(doc)
| let host: String => env.out.print(host)
| JsonNotFound => env.out.print("no host configured")
end

// Modify (returns new document with the change applied)
match host_lens.set(doc, "newhost.example.com")
| let updated: JsonValue => // updated doc
| JsonNotFound => // path didn't exist
end
```


```pony
class val JsonLens
```

## Constructors

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


Create an identity lens (focuses on the root value).

```pony
new val create()
: JsonLens val^
```

#### Returns

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

---

## Public Functions

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


Compose a navigation step onto this lens.

```pony
fun box apply(
  key_or_index: (String val | USize val))
: JsonLens val
```
#### Parameters

*   key_or_index: ([String](builtin-String.md) val | [USize](builtin-USize.md) val)

#### Returns

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

---

### get
<span class="source-link">[[Source]](src/json/json_lens.md#L-0-41)</span>


Apply this lens to read a value.

```pony
fun box get(
  input: (JsonObject val | JsonArray val | String val | 
    I64 val | F64 val | Bool val | 
    None val))
: (JsonObject val | JsonArray val | String val | 
    I64 val | F64 val | Bool val | 
    None val | JsonNotFound val)
```
#### Parameters

*   input: ([JsonObject](json-JsonObject.md) val | [JsonArray](json-JsonArray.md) val | [String](builtin-String.md) val | 
    [I64](builtin-I64.md) val | [F64](builtin-F64.md) val | [Bool](builtin-Bool.md) val | 
    [None](builtin-None.md) val)

#### Returns

* ([JsonObject](json-JsonObject.md) val | [JsonArray](json-JsonArray.md) val | [String](builtin-String.md) val | 
    [I64](builtin-I64.md) val | [F64](builtin-F64.md) val | [Bool](builtin-Bool.md) val | 
    [None](builtin-None.md) val | [JsonNotFound](json-JsonNotFound.md) val)

---

### set
<span class="source-link">[[Source]](src/json/json_lens.md#L-0-45)</span>


Apply this lens to update a value, returning a new root.
Returns JsonNotFound if the path doesn't exist.


```pony
fun box set(
  input: (JsonObject val | JsonArray val | String val | 
    I64 val | F64 val | Bool val | 
    None val),
  value: (JsonObject val | JsonArray val | String val | 
    I64 val | F64 val | Bool val | 
    None val))
: (JsonObject val | JsonArray val | String val | 
    I64 val | F64 val | Bool val | 
    None val | JsonNotFound val)
```
#### Parameters

*   input: ([JsonObject](json-JsonObject.md) val | [JsonArray](json-JsonArray.md) val | [String](builtin-String.md) val | 
    [I64](builtin-I64.md) val | [F64](builtin-F64.md) val | [Bool](builtin-Bool.md) val | 
    [None](builtin-None.md) val)
*   value: ([JsonObject](json-JsonObject.md) val | [JsonArray](json-JsonArray.md) val | [String](builtin-String.md) val | 
    [I64](builtin-I64.md) val | [F64](builtin-F64.md) val | [Bool](builtin-Bool.md) val | 
    [None](builtin-None.md) val)

#### Returns

* ([JsonObject](json-JsonObject.md) val | [JsonArray](json-JsonArray.md) val | [String](builtin-String.md) val | 
    [I64](builtin-I64.md) val | [F64](builtin-F64.md) val | [Bool](builtin-Bool.md) val | 
    [None](builtin-None.md) val | [JsonNotFound](json-JsonNotFound.md) val)

---

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


Apply this lens to remove a value, returning a new root.
Returns JsonNotFound if the path doesn't exist.


```pony
fun box remove(
  input: (JsonObject val | JsonArray val | String val | 
    I64 val | F64 val | Bool val | 
    None val))
: (JsonObject val | JsonArray val | String val | 
    I64 val | F64 val | Bool val | 
    None val | JsonNotFound val)
```
#### Parameters

*   input: ([JsonObject](json-JsonObject.md) val | [JsonArray](json-JsonArray.md) val | [String](builtin-String.md) val | 
    [I64](builtin-I64.md) val | [F64](builtin-F64.md) val | [Bool](builtin-Bool.md) val | 
    [None](builtin-None.md) val)

#### Returns

* ([JsonObject](json-JsonObject.md) val | [JsonArray](json-JsonArray.md) val | [String](builtin-String.md) val | 
    [I64](builtin-I64.md) val | [F64](builtin-F64.md) val | [Bool](builtin-Bool.md) val | 
    [None](builtin-None.md) val | [JsonNotFound](json-JsonNotFound.md) val)

---

### compose
<span class="source-link">[[Source]](src/json/json_lens.md#L-0-59)</span>


Sequential composition: navigate this lens, then the other.

```pony
fun box compose(
  other: JsonLens val)
: JsonLens val
```
#### Parameters

*   other: [JsonLens](json-JsonLens.md) val

#### Returns

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

---

### or_else
<span class="source-link">[[Source]](src/json/json_lens.md#L-0-63)</span>


Choice: try this lens, fall back to alt if JsonNotFound.

```pony
fun box or_else(
  alt: JsonLens val)
: JsonLens val
```
#### Parameters

*   alt: [JsonLens](json-JsonLens.md) val

#### Returns

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

---

