# Moshi.jl and matching a dictionary as a pattern

**URL:** <https://discourse.julialang.org/t/moshi-jl-and-matching-a-dictionary-as-a-pattern/134677>\
**Category:** General Usage\
**Tags:** question\
**Created:** [December 22, 2025, 12:55am UTC](https://discourse.julialang.org/t/moshi-jl-and-matching-a-dictionary-as-a-pattern/134677 "2025-12-22T00:55:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pedro](https://avatars.discourse-cdn.com/v4/letter/p/919ad9/32.png) [@Pedro](https://discourse.julialang.org/u/Pedro)\
**Post date:** [December 22, 2025, 12:55am UTC](https://discourse.julialang.org/t/moshi-jl-and-matching-a-dictionary-as-a-pattern/134677/1 "2025-12-22T00:55:46Z")

</div>

I am currently porting to Julia some Python code that contains a lot of pattern matching for decoding JSON messages received via network socket, which are represented conveniently in Python as dictionaries.

Since Julia doesn’t have pattern matching in `Base`, I have decided to use Moshi.jl, since it looks like the most recommended implementation. However, it seems that it doesn’t support `Dict` as a pattern. Minimal working example:

```julia
using Moshi.Match: @match

msg = Dict(
   "error" => 2,
)

@match msg begin
   Dict("error" => x) => x
   _ => "unknown message"
end

```

The `@match` matches a wildcard pattern, i.e., returns string “unknown message”, but to my understanding it should match the `Dict` and return `2`.

Am I doing something wrong here?

---

<div class="post-metadata">

**Author:** ![tecosaur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tecosaur/32/23206_2.png) [@tecosaur](https://discourse.julialang.org/u/tecosaur)\
**Post date:** [December 22, 2025, 5:58am UTC](https://discourse.julialang.org/t/moshi-jl-and-matching-a-dictionary-as-a-pattern/134677/2 "2025-12-22T05:58:24Z")

</div>

I will just comment that all of the dict-values you want to match follow this pattern, you could just do

```julia-auto
@something(get(msg, "error", nothing),
           get(...),
           ...,
           "unknown message")

```

---

<div class="post-metadata">

**Author:** ![Pedro](https://avatars.discourse-cdn.com/v4/letter/p/919ad9/32.png) [@Pedro](https://discourse.julialang.org/u/Pedro)\
**Post date:** [December 31, 2025, 10:14pm UTC](https://discourse.julialang.org/t/moshi-jl-and-matching-a-dictionary-as-a-pattern/134677/3 "2025-12-31T22:14:34Z")

</div>

Indeed, I could do it like that in presented simple case, however, I am not so sure if it will work for more complex cases.

And it’s still baffles me, if it’s somehow possible with Moshi pattern matching syntax, since it’s more readable, at least for me.

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [January 1, 2026, 2:06am UTC](https://discourse.julialang.org/t/moshi-jl-and-matching-a-dictionary-as-a-pattern/134677/4 "2026-01-01T02:06:09Z")

</div>

@Roger-luo would know best.

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [January 1, 2026, 4:41am UTC](https://discourse.julialang.org/t/moshi-jl-and-matching-a-dictionary-as-a-pattern/134677/5 "2026-01-01T04:41:13Z")

</div>

Ironically, the spiritual predecessor MLStyle.jl can do this, [documented here](https://thautwarm.github.io/MLStyle.jl/latest/syntax/pattern.html#destructuring-tuples-arrays-and-dictionaries-with-pattern-matching).

```julia-auto
julia> Moshi.Match.@match msg begin
          Dict("error" => x) => x
          _ => "unknown message"
       end
"unknown message"

julia> MLStyle.@match msg begin
          Dict("error" => x) => x
          _ => "unknown message"
       end
2

```

I don’t use either so I can’t rule out Moshi doing this some other way, couldn’t find it in the docs though.
