# How to speed up converting Dict to Json?

**URL:** <https://discourse.julialang.org/t/how-to-speed-up-converting-dict-to-json/124061>\
**Category:** General Usage\
**Tags:** question, json\
**Created:** [December 21, 2024, 1:25pm UTC](https://discourse.julialang.org/t/how-to-speed-up-converting-dict-to-json/124061 "2024-12-21T13:25:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Xing\_Shi\_Cai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xing_shi_cai/32/14128_2.png) [@Xing\_Shi\_Cai](https://discourse.julialang.org/u/Xing_Shi_Cai)\
**Post date:** [December 21, 2024, 1:25pm UTC](https://discourse.julialang.org/t/how-to-speed-up-converting-dict-to-json/124061/1 "2024-12-21T13:25:31Z")

</div>

I am using Julia and the `JSON` module to convert a `Dict` to JSON and sent it to LLM APIs. Below is the code I’m using:

```julia
function send_request(url, headers, payload)
    try
        @debug "Payload" payload
        json_payload = JSON.json(payload)
        @debug "JSON payload ready"
        response = HTTP.request("POST", url, headers, json_payload, proxy=ENV["http_proxy"])

        if response.status == 200
            return response
        else
            @error "Request failed with status: $(response.status)"
            println(String(response.body))
            return nothing
        end
    catch http_error
        @error "HTTP request error: $http_error"
        return nothing
    end
end

```

Normally, this works fine. However, when the `json_payload` contains a field that is the base64 encoding of an image (around 2MB in size), the call to `JSON.json(payload)` takes a very long time to complete.

Is there any way to speed up the conversion process when dealing with large base64-encoded data in the JSON payload?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [December 21, 2024, 1:35pm UTC](https://discourse.julialang.org/t/how-to-speed-up-converting-dict-to-json/124061/2 "2024-12-21T13:35:30Z")

</div>

> However, when the `json_payload` contains a field that is the base64 encoding of an image (around 2MB in size), the call to `JSON.json(payload)` takes a very long time to complete.

Can you provide a reproducible example with data? What do you consider a “very long time”?

I tried running `JSON.json` on a `Dict` containing a 2MB string, and it takes 20ms.

```julia
julia> using JSON, Random, BenchmarkTools

julia> payload = Dict("foo" => Random.randstring(2^21)); # 2MB string

julia> @btime JSON.json($payload);
  21.234 ms (35 allocations: 4.44 MiB)

```

It’s going to be hard for anyone to help you without more information. Something like the above, where you provide code to generate a `Dict` containing random data, would be easiest to reproduce.

---

<div class="post-metadata">

**Author:** ![Xing\_Shi\_Cai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xing_shi_cai/32/14128_2.png) [@Xing\_Shi\_Cai](https://discourse.julialang.org/u/Xing_Shi_Cai)\
**Post date:** [December 21, 2024, 2:12pm UTC](https://discourse.julialang.org/t/how-to-speed-up-converting-dict-to-json/124061/3 "2024-12-21T14:12:03Z")

</div>

Sorry. It seems that the problem is not JSON, but the LLM API is a bit too slow to dealing with such a large payload. For some reason the `println` I used to track where it is slow didn’t accurately show where the bottleneck is.
