# HOWTO: JSON3 read json path value as Custom type or Convert JSON3.Object as Custom type

**URL:** https://discourse.julialang.org/t/howto-json3-read-json-path-value-as-custom-type-or-convert-json3-object-as-custom-type/115014
**Category:** New to Julia
**Tags:** question
**Created:** [May 31, 2024, 2:15pm UTC](https://discourse.julialang.org/t/howto-json3-read-json-path-value-as-custom-type-or-convert-json3-object-as-custom-type/115014 "2024-05-31T14:15:25Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![listenmmm](https://avatars.discourse-cdn.com/v4/letter/l/e274bd/32.png) [@listenmmm](https://discourse.julialang.org/u/listenmmm)
#### Post date: [May 31, 2024, 2:15pm UTC](https://discourse.julialang.org/t/howto-json3-read-json-path-value-as-custom-type-or-convert-json3-object-as-custom-type/115014/1 "2024-05-31T14:15:25Z")

</div>

```julia-auto
begin
  struct Book
    name
    price
    tags
  end

  StructTypes.StructType(::Type{Book}) = StructTypes.Struct()

  request_body_io = IOBuffer()
  response_body_io = IOBuffer()

  JSON3.write(request_body_io, Book("the julia", 11.11, ["programming", "julia"]))
  request_body_io |> seekstart

  response = request(
    "https://httpbin.org/post",
    method="POST",
    headers=Dict("accept" => "application/json", "content-type" => "application/json"),
    verbose=false,
    input=request_body_io,
    output=response_body_io,
  )

  @show response.status
  @show response.message

  body = response_body_io |> seekstart |> JSON3.read
  # => how to convert the JSON3.Object as type Book or how to read json path (e.g. root.json) and convert it's value as Type Book directly
  #
  @show body["json"]

end

```

# UPDATE (provided by @Jeff_Emanuel )

```julia-auto
  book = response_body_io |> seekstart |> JSON3.read |> x -> StructTypes.constructfrom(Book, x["json"])
  @show typeof(book) # => typeof(book) = Book
  @show book # => book = Book("the julia", 11.11, ["programming", "julia"])

```

---

<div class="post-metadata">

### Author: ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)
#### Post date: [May 31, 2024, 2:23pm UTC](https://discourse.julialang.org/t/howto-json3-read-json-path-value-as-custom-type-or-convert-json3-object-as-custom-type/115014/2 "2024-05-31T14:23:22Z")

</div>

[https://quinnj.github.io/JSON3.jl/stable/#Read-JSON-into-a-type](https://quinnj.github.io/JSON3.jl/stable/#Read-JSON-into-a-type)

---

<div class="post-metadata">

### Author: ![listenmmm](https://avatars.discourse-cdn.com/v4/letter/l/e274bd/32.png) [@listenmmm](https://discourse.julialang.org/u/listenmmm)
#### Post date: [May 31, 2024, 2:27pm UTC](https://discourse.julialang.org/t/howto-json3-read-json-path-value-as-custom-type-or-convert-json3-object-as-custom-type/115014/3 "2024-05-31T14:27:11Z")

</div>

I’ve looked at this but couldn’t find what I was looking for, or I didn’t know how to apply it to my needs

---

<div class="post-metadata">

### Author: ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)
#### Post date: [May 31, 2024, 2:28pm UTC](https://discourse.julialang.org/t/howto-json3-read-json-path-value-as-custom-type-or-convert-json3-object-as-custom-type/115014/4 "2024-05-31T14:28:51Z")

</div>

Use the 2-arg JSON3.read which takes a type instead of the single argument JSON3.read you have at the end of your code. Something like

```julia
body = JSON3.read( response_body_io |> seekstart, Book )

```

You might need to read the body into a String first.

---

<div class="post-metadata">

### Author: ![listenmmm](https://avatars.discourse-cdn.com/v4/letter/l/e274bd/32.png) [@listenmmm](https://discourse.julialang.org/u/listenmmm)
#### Post date: [May 31, 2024, 2:30pm UTC](https://discourse.julialang.org/t/howto-json3-read-json-path-value-as-custom-type-or-convert-json3-object-as-custom-type/115014/5 "2024-05-31T14:30:41Z")

</div>

I don’t want to convert the entire response body to type Book, but to convert its json field (root.json) to type Book

---

<div class="post-metadata">

### Author: ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)
#### Post date: [May 31, 2024, 2:33pm UTC](https://discourse.julialang.org/t/howto-json3-read-json-path-value-as-custom-type-or-convert-json3-object-as-custom-type/115014/6 "2024-05-31T14:33:53Z")

</div>

If you don’t have a type for the entire JSON structure, it may be simplest (if not fastest) to re-encode just the part that constitutes a book and the re-decode that as a Book. I’d look at defining a type for the entire structure.

---

<div class="post-metadata">

### Author: ![listenmmm](https://avatars.discourse-cdn.com/v4/letter/l/e274bd/32.png) [@listenmmm](https://discourse.julialang.org/u/listenmmm)
#### Post date: [May 31, 2024, 2:37pm UTC](https://discourse.julialang.org/t/howto-json3-read-json-path-value-as-custom-type-or-convert-json3-object-as-custom-type/115014/7 "2024-05-31T14:37:19Z")

</div>

could JSON3 Intermediate Type（e.g. JSON3.Object) convert as custom type ?

---

<div class="post-metadata">

### Author: ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)
#### Post date: [May 31, 2024, 2:39pm UTC](https://discourse.julialang.org/t/howto-json3-read-json-path-value-as-custom-type-or-convert-json3-object-as-custom-type/115014/8 "2024-05-31T14:39:48Z")

</div>

I’m not aware of any way to do that automatically. I haven’t used this, but you could experiment with [Home · StructTypes.jl](https://juliadata.org/StructTypes.jl/stable/#StructTypes.constructfrom)

---

<div class="post-metadata">

### Author: ![listenmmm](https://avatars.discourse-cdn.com/v4/letter/l/e274bd/32.png) [@listenmmm](https://discourse.julialang.org/u/listenmmm)
#### Post date: [May 31, 2024, 3:00pm UTC](https://discourse.julialang.org/t/howto-json3-read-json-path-value-as-custom-type-or-convert-json3-object-as-custom-type/115014/9 "2024-05-31T15:00:37Z")

</div>

It works fine, thanks!

I don’t know if this takes advantage of the lazy parse feature of JSON3.
