# How to get and pass on json retrieved via HTTP

**URL:** <https://discourse.julialang.org/t/how-to-get-and-pass-on-json-retrieved-via-http/64532>\
**Category:** Web Stack\
**Created:** [July 12, 2021, 8:32pm UTC](https://discourse.julialang.org/t/how-to-get-and-pass-on-json-retrieved-via-http/64532 "2021-07-12T20:32:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jon\_Norberg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jon_norberg/32/3218_2.png) [@Jon\_Norberg](https://discourse.julialang.org/u/Jon_Norberg)\
**Post date:** [July 12, 2021, 8:32pm UTC](https://discourse.julialang.org/t/how-to-get-and-pass-on-json-retrieved-via-http/64532/1 "2021-07-12T20:32:51Z")

</div>

Hi, I am getting my feet wet in web-interactivity…so this might be a very basic question.

In the following code the retrieved messages are nicely printed out as a string containing the json information, but my JSON3.read does not work.

```julia
using HTTP, JSON3

io = Base.BufferStream()
ch=Channel{Dict}(10)
@async while !eof(io)
    bytes = readavailable(io)
    println(String(bytes))
    put!(ch,JSON3.read(String(bytes)))
end

@async HTTP.request("GET", "https://username:password@bluemix.cloudantnosqldb.appdomain.cloud/test/_changes?feed=continuous&since=now", response_stream=io)

```

I have also tried to make a version saving the string directly:

```julia
io = Base.BufferStream()
ch=Channel{String}(10)
@async while !eof(io)
    bytes = readavailable(io)
    println(String(bytes))
    put!(ch,String(bytes))
end

```

but this ends up only saving something like "" to ch, while the println still outputs the full json. I realise this may have less with webstack to do than conversion of types but I figured this is where people know how to do it best.

Its a bit hard to do a MWE as I don’t have an open couchDB server to set up at the moment…

Thanks for any pointers!

---

<div class="post-metadata">

**Author:** ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)\
**Post date:** [July 12, 2021, 9:19pm UTC](https://discourse.julialang.org/t/how-to-get-and-pass-on-json-retrieved-via-http/64532/2 "2021-07-12T21:19:50Z")

</div>

`String` is destructive, it empties your buffer. You can do something like

```julia
io = Base.BufferStream()
ch=Channel{Dict}(10)
@async while !eof(io)
    bytes = readavailable(io)
    text = String(bytes)    
    println(text)
    put!(ch, JSON3.read(text))
end

```

---

<div class="post-metadata">

**Author:** ![Jon\_Norberg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jon_norberg/32/3218_2.png) [@Jon\_Norberg](https://discourse.julialang.org/u/Jon_Norberg)\
**Post date:** [July 13, 2021, 5:13am UTC](https://discourse.julialang.org/t/how-to-get-and-pass-on-json-retrieved-via-http/64532/3 "2021-07-13T05:13:48Z")

</div>

That’s it, thanks. Its these little details that take time to understand :-). Since I get an empty line in-between also I had to adjust as so also:

```julia
io = Base.BufferStream()
ch=Channel{Dict}(10)
@async while !eof(io)
    bytes = readavailable(io)
    text = String(bytes)    
    println(text)
    if length(text) > 0 && text[1] == '{'
        put!(ch, JSON3.read(text))
    end
end

```

---

<div class="post-metadata">

**Author:** ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)\
**Post date:** [July 13, 2021, 5:36am UTC](https://discourse.julialang.org/t/how-to-get-and-pass-on-json-retrieved-via-http/64532/4 "2021-07-13T05:36:49Z")

</div>

While `length(text) > 0` is fine, but more common is `!isempty(text)`. It can be more performant if `length` is costly and also it conveys better your intention.

---

<div class="post-metadata">

**Author:** ![Jon\_Norberg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jon_norberg/32/3218_2.png) [@Jon\_Norberg](https://discourse.julialang.org/u/Jon_Norberg)\
**Post date:** [July 13, 2021, 5:46am UTC](https://discourse.julialang.org/t/how-to-get-and-pass-on-json-retrieved-via-http/64532/5 "2021-07-13T05:46:55Z")

</div>

Changed! Thanks!
