# How to modify a JSON object that's been read in?

**URL:** https://discourse.julialang.org/t/how-to-modify-a-json-object-thats-been-read-in/68775
**Category:** Data
**Created:** [September 26, 2021, 1:15pm UTC](https://discourse.julialang.org/t/how-to-modify-a-json-object-thats-been-read-in/68775 "2021-09-26T13:15:16Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)
#### Post date: [September 26, 2021, 1:15pm UTC](https://discourse.julialang.org/t/how-to-modify-a-json-object-thats-been-read-in/68775/1 "2021-09-26T13:15:16Z")

</div>

I have read a JSON using JSON3.jl but after that I can’t seem to modify the json.

E.g.

```julia
a = JSON3.read("{\"a\":1}")

a[:b] = 2

```

what I get is this.

Is there an easy to mutate the object?

```julia
LoadError: MethodError: no method matching setindex!(::JSON3.Object{Base.CodeUnits{UInt8, String}, Vector{UInt64}}, ::Int64, ::Symbol)
e[0mClosest candidates are:
e[0m setindex!(::AbstractDict, ::Any, ::Any, e[91m::Anye[39m, e[91m::Any...e[39m) at abstractdict.jl:505
MethodError: no method matching setindex!(::JSON3.Object{Base.CodeUnits{UInt8, String}, Vector{UInt64}}, ::Int64, ::Symbol)
Closest candidates are:
  setindex!(::AbstractDict, ::Any, ::Any, ::Any, ::Any...) at abstractdict.jl:505

Stacktrace:
 [1] top-level scope
   @ In[139]:3
 [2] eval
   @ ./boot.jl:360 [inlined]
 [3] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
   @ Base ./loading.jl:1094

```

---

<div class="post-metadata">

### Author: ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)
#### Post date: [September 26, 2021, 1:53pm UTC](https://discourse.julialang.org/t/how-to-modify-a-json-object-thats-been-read-in/68775/2 "2021-09-26T13:53:58Z")

</div>

As per the [docs](https://quinnj.github.io/JSON3.jl/stable/#Builtin-types):

> The `JSON3.Object` supports the `AbstractDict` interface, but is read-only (it represents a _view_ into the JSON source input), thus it supports `obj[:x]` and `obj["x"]` , as well as `obj.x` for accessing fields. It supports `keys(obj)` to see available keys in the object structure. You can call `length(obj)` to see how many key-value pairs there are, and it iterates `(k, v)` pairs like a normal `Dict` . It also supports the regular `get(obj, key, default)` family of methods.
> 
> The `JSON3.Array{T}` supports the `AbstractArray` interface, but like `JSON3.Object` is a _view_ into the input JSON, hence is read-only. It supports normal array methods like `length(A)` , `size(A)` , iteration, and `A[i]` `getindex` methods.
> 
> If you really need `Dict` s and `Vector` s, then you can use `copy(x)` to recursively convert `JSON3.Object` s to `Dict` s and `JSON3.Array` s to `Vector` s.

---

<div class="post-metadata">

### Author: ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)
#### Post date: [September 27, 2021, 1:04am UTC](https://discourse.julialang.org/t/how-to-modify-a-json-object-thats-been-read-in/68775/3 "2021-09-27T01:04:45Z")

</div>

Just use `copy` to turn it into something mutable

```julia
using JSON3
a = JSON3.read("{\"a\":1}")

a = copy(a)

a[:b] = 2

```
