# Fastest JSON parser to julia

**URL:** <https://discourse.julialang.org/t/fastest-json-parser-to-julia/49639>\
**Category:** Specific Domains\
**Tags:** ccall, json, cwrap\
**Created:** [November 5, 2020, 7:32pm UTC](https://discourse.julialang.org/t/fastest-json-parser-to-julia/49639 "2020-11-05T19:32:58Z")\
**Posts on this page:** 1\
**Showing post:** 6

<div class="post-metadata">

**Author:** ![laborg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laborg/32/5474_2.png) [@laborg](https://discourse.julialang.org/u/laborg)\
**Post date:** [November 6, 2020, 10:07am UTC](https://discourse.julialang.org/t/fastest-json-parser-to-julia/49639/6 "2020-11-06T10:07:16Z")

</div>

**Update** : My implementation isn’t correct as it assumed ordered fields.

~~The implementation of the json benchmark looks not very favorable~~  
~~Compare to the native facility of using StructTypes~~ :

```julia
using JSON3
using StructTypes

struct Coordinate
    x::Float64
    y::Float64
    z::Float64
end

struct Coordinates
    arr::Vector{Coordinate}
end

StructTypes.StructType(::Type{Coordinate}) = StructTypes.Struct()
StructTypes.StructType(::Type{Coordinates}) = StructTypes.Struct()

function calc(text)
    jobj = JSON3.read(text)
    coordinates = jobj["coordinates"]
    len = length(coordinates)
    x = y = z = 0

    for coord in coordinates
        x += coord["x"]
        y += coord["y"]
        z += coord["z"]
    end

    Coordinate(x / len, y / len, z / len)
end

function calc_struct(text)
    coordinates = JSON3.read(text,Coordinates)
    
    len = length(coordinates.arr)
    x = y = z = 0

    for coord in coordinates.arr
        x += coord.x
        y += coord.y
        z += coord.z
    end

    Coordinate(x / len, y / len, z / len)
end

```

leads to:

```julia-repl
julia> @btime calc(text)
  3.314 μs (20 allocations: 1.14 KiB)
Coordinate(2.0, 0.5, 0.25)

julia> @btime calc_struct(text)
  297.748 ns (11 allocations: 496 bytes)
Coordinate(2.0, 0.5, 0.25)

```

---

_[View the full topic](https://discourse.julialang.org/t/fastest-json-parser-to-julia/49639)._
