Search code examples
jsonstringgostdin

Read json object from stdin in golang


How to read a json object from stdin? I want to copy and paste a json object into the stdin, read it and unmarshal it. Here is the code:

    var input string
    _, err := fmt.Scan(&input)
    if err != nil {
        fmt.Println(err)
        continue
    }

    var record MedicalRecord
    if err := json.Unmarshal([]byte(input), &record); err != nil {
        log.Println(err)
        continue
    }

And the errors are printed to console.

> 2018/06/26 00:26:32 invalid character ':' after top-level value
> 2018/06/26 00:26:32 unexpected end of JSON input
> 2018/06/26 00:26:32 invalid character ':' after top-level value
> 2018/06/26 00:26:32 invalid character ',' after top-level value
> 2018/06/26 00:26:32 invalid character ':' after top-level value
> 2018/06/26 00:26:32 invalid character ',' after top-level value
> 2018/06/26 00:26:32 invalid character ':' after top-level value
> 2018/06/26 00:26:32 invalid character ',' after top-level value
> 2018/06/26 00:26:32 invalid character ':' after top-level value
> 2018/06/26 00:26:32 invalid character ',' after top-level value

If I'm not mistaken, Go is reading until it finds '\n' . How can I solve this problem?


Solution

  • Use a *json.Decoder to consume JSON from an io.Reader:

    package main
    
    import (
        "encoding/json"
        "log"
        "os"
    )
    
    type MedicalRecord struct{}
    
    func main() {
        var record MedicalRecord
    
        err := json.NewDecoder(os.Stdin).Decode(&record)
        if err != nil {
            log.Fatal(err)
        }
    }
    

    You can consume multiple consecutive JSON documents by calling Decode repeatedly:

    package main
    
    import (
        "encoding/json"
        "io"
        "log"
        "os"
    )
    
    type MedicalRecord struct{}
    
    func main() {
        var record MedicalRecord
    
        dec := json.NewDecoder(os.Stdin)
        for {
            err := dec.Decode(&record)
            if err == io.EOF {
                return
            }
            if err != nil {
                log.Fatal(err)
            }
        }
    }