# Position info when decoding JSON?

**URL:** <https://discourse.elm-lang.org/t/position-info-when-decoding-json/5179>\
**Category:** Learn\
**Created:** [February 14, 2020, 2:30pm UTC](https://discourse.elm-lang.org/t/position-info-when-decoding-json/5179 "2020-02-14T14:30:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rupert](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/rupert/32/1775_2.png) [@rupert](https://discourse.elm-lang.org/u/rupert)\
**Post date:** [February 14, 2020, 2:30pm UTC](https://discourse.elm-lang.org/t/position-info-when-decoding-json/5179/1 "2020-02-14T14:30:30Z")

</div>

Hi,

I am decoding a largish file of JSON, and would like to give nice error messages. If I use a `Json.Decode.Decoder` there is no context describing the position within the JSON that could be used to help with this.

For example, I might like an error message like:

```auto
The field 'date' must be in ISO format (like 2020-01-30):

204| someRecord : {
205| date : 1234,
            ^^^^^^^^^^^
206| ...

```

I think to do this, I would need to write a `Parser` for JSON from scratch. Never actually looked at the code, but I suspect that the String -\> JSON part happens in javascript, and Elm works with the in-memory JSON after that.

If I can’t do that, perhaps an easier way might be to track the positional context through the structure of the JSON, to give an error message like this:

```auto
The field 'date' must be in ISO format (like 2020-01-30):

At 'someArray[5].someRecord.date', the value given is 1234, which is not in ISO format.

```

Any ideas how to accomplish this? Is there some transformation that can be built on top of a Decoder to always give this context, or perhaps even a package that already does this?

---

<div class="post-metadata">

**Author:** ![klaftertief](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/klaftertief/32/5338_2.png) [@klaftertief](https://discourse.elm-lang.org/u/klaftertief)\
**Post date:** [February 14, 2020, 2:42pm UTC](https://discourse.elm-lang.org/t/position-info-when-decoding-json/5179/2 "2020-02-14T14:42:49Z")

</div>

Have you seen [https://package.elm-lang.org/packages/zwilias/json-decode-exploration/latest/](https://package.elm-lang.org/packages/zwilias/json-decode-exploration/latest/)?  
I have not used it, but I believe it gives you the tools to do what you want.

---

<div class="post-metadata">

**Author:** ![rupert](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/rupert/32/1775_2.png) [@rupert](https://discourse.elm-lang.org/u/rupert)\
**Post date:** [February 14, 2020, 2:46pm UTC](https://discourse.elm-lang.org/t/position-info-when-decoding-json/5179/3 "2020-02-14T14:46:31Z")

</div>

Yes, just looking at that. I think it would give me the descriptive path solution ( someArray[5].someRecord.someField ).

I am also looking at this one, and wondering if it could be upgraded to `Parser.Advanced` and report line numbers:

[https://package.elm-lang.org/packages/allenap/elm-json-decode-broken/latest/Json-Decode-Broken](https://package.elm-lang.org/packages/allenap/elm-json-decode-broken/latest/Json-Decode-Broken)

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/elm_lang/original/1X/50a05e53677a2c3b47776d7abd0f113eb50193a1.png) [@system](https://discourse.elm-lang.org/u/system)\
**Post date:** [February 24, 2020, 2:57pm UTC](https://discourse.elm-lang.org/t/position-info-when-decoding-json/5179/4 "2020-02-24T14:57:27Z")

</div>

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.
