# How to parse a JSON object?

**URL:** <https://discourse.elm-lang.org/t/how-to-parse-a-json-object/4977>\
**Category:** Learn\
**Created:** [January 11, 2020, 4:58pm UTC](https://discourse.elm-lang.org/t/how-to-parse-a-json-object/4977 "2020-01-11T16:58:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Viir](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/viir/32/4103_2.png) [@Viir](https://discourse.elm-lang.org/u/Viir)\
**Post date:** [January 11, 2020, 4:58pm UTC](https://discourse.elm-lang.org/t/how-to-parse-a-json-object/4977/1 "2020-01-11T16:58:18Z")

</div>

I have a `String` containing a JSON object and want to get the content of a property of this JSON object.

The input String could look like this:

```auto
"{\"fieldName\":4652007308841189719}"

```

I tried to use the [`elm/json/1.1.3` package](https://package.elm-lang.org/packages/elm/json/1.1.3) to parse the JSON, but only got wrong results using this implementation:

```auto
module Main exposing (main)

import Browser
import Html
import Json.Decode
import Json.Encode

getJsonSubstringFromField fieldName =
    Json.Decode.decodeString (Json.Decode.field fieldName Json.Decode.value)
        >> Result.map (Json.Encode.encode 0)

main : Html.Html a
main =
    "{\"fieldName\":4652007308841189719}"
        |> getJsonSubstringFromField "fieldName"
        |> Result.withDefault "Failed to decode JSON"
        |> Html.text

```

([https://ellie-app.com/7Kk2TrLn3qfa1](https://ellie-app.com/7Kk2TrLn3qfa1))

Instead of the expected value, this displayed `4652007308841189000`.

How can I get the JSON value of the property of an object? The original substring would work for me, but preserving formatting is not necessary.

* * *

Edit:  
The solution was to use a JSON parser like [https://github.com/allenap/elm-json-decode-broken/blob/4b1861f4282b00146ae57a48a5ba0dae442baefb/src/Json/Decode/Broken.elm](https://github.com/allenap/elm-json-decode-broken/blob/4b1861f4282b00146ae57a48a5ba0dae442baefb/src/Json/Decode/Broken.elm)  
The readme in the repository there is more focused on broken JSON, but as @dmy showed, we can configure it to help us parse valid JSON:  
([https://ellie-app.com/7KBMdXHBKVba1](https://ellie-app.com/7KBMdXHBKVba1))

```auto
module Main exposing (main)

import Browser
import Html
import Json.Decode
import Json.Decode.Broken as JsonParser
import Json.Encode
import Parser exposing (Parser)

parseNumberToJsonString : Parser Json.Encode.Value
parseNumberToJsonString =
    Parser.chompWhile Char.isDigit
        |> Parser.getChompedString
        |> Parser.map Json.Encode.string

parseJsonMappingNumbersToStrings : String -> Result Json.Decode.Error Json.Decode.Value
parseJsonMappingNumbersToStrings str =
    let
        (JsonParser.Config defaultConfig) =
            JsonParser.defaultConfig
    in
    str
        |> JsonParser.parseWith (JsonParser.Config { defaultConfig | number = parseNumberToJsonString })
        |> Result.mapError (\err -> Json.Decode.Failure "" Json.Encode.null)

main : Html.Html a
main =
    "{\"fieldName\":4652007308841189719}"
        |> parseJsonMappingNumbersToStrings
        |> Result.andThen (Json.Decode.decodeValue (Json.Decode.field "fieldName" Json.Decode.string))
        |> Result.withDefault "Failed to decode JSON"
        |> Html.text

```

* * *

---

<div class="post-metadata">

**Author:** ![dmy](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/dmy/32/702_2.png) [@dmy](https://discourse.elm-lang.org/u/dmy)\
**Post date:** [January 11, 2020, 5:23pm UTC](https://discourse.elm-lang.org/t/how-to-parse-a-json-object/4977/2 "2020-01-11T17:23:46Z")

</div>

The integer is more than `2^53 - 1` [maxSafeInteger](https://package.elm-lang.org/packages/elm-community/basics-extra/latest/Basics-Extra#maxSafeInteger), so it cannot be represented precisely in Elm or JavaScript. `4652007308841189000` is its float representation in JavaScript, losing the precision of the last 3 digits.

Try pasting the number in your browser JavaScript console, you will get the same result.

If you cannot change the JSON format (for example using a string instead of an integer for such numbers), you can either modify the string before decoding it, or use a custom parser, for example using [allenap/elm-json-decode-broken](https://package.elm-lang.org/packages/allenap/elm-json-decode-broken/latest/), decoding numbers as strings (ex: [https://ellie-app.com/7Kmcsgq79yqa1](https://ellie-app.com/7Kmcsgq79yqa1)).

---

<div class="post-metadata">

**Author:** ![Viir](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/viir/32/4103_2.png) [@Viir](https://discourse.elm-lang.org/u/Viir)\
**Post date:** [January 12, 2020, 7:41am UTC](https://discourse.elm-lang.org/t/how-to-parse-a-json-object/4977/3 "2020-01-12T07:41:35Z")

</div>

> [@dmy](#):
>
> If you cannot change the JSON format (for example using a string instead of an integer for such numbers), you can either modify the string before decoding it, or use a custom parser, for example using [allenap/elm-json-decode-broken](https://package.elm-lang.org/packages/allenap/elm-json-decode-broken/latest/), decoding numbers as strings (ex: [https://ellie-app.com/7Kmcsgq79yqa1](https://ellie-app.com/7Kmcsgq79yqa1)).

Thank you, that solved it!

---

<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:** [January 22, 2020, 7:41am UTC](https://discourse.elm-lang.org/t/how-to-parse-a-json-object/4977/4 "2020-01-22T07:41:39Z")

</div>

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