FAQ
New to GO,

I am reading in a json from post, I tried desperately to try to use a
dynamic map to interface to deal with the uncertainty of whether it is a
json array or single object, to no luck, so trying it the "Right" way and
building a struct of the expected element(s) but how do I know if to
json.Unmarshal to a struct or an array of structs?
Or do I try unmarshal both and what doesn't err, keep?

func PostStatement(w http.ResponseWriter, r *http.Request){

body, err := ioutil.ReadAll(r.Body)
var statements []Statement //but what if json is a single statement
err = json.Unmarshal([]byte(body), &statements)
...

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Ibrahim M. Ghazal at Aug 6, 2013 at 6:10 am
    the uncertainty of whether it is a json array or single object
    How is the JSON generated? Do you have control over it? I think it
    might be easier to make the JSON consistent.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Tobias Contreras at Aug 7, 2013 at 7:54 pm
    Unfortunately I don't, I tried but no luck.
    Should I try json decoding to array of structs, if fails, then bind to
    single struct or parse the body as string, check first character if not '['
    then wrap string in brackets and pass through to json decoder to array of
    structs
    On Aug 6, 2013 12:10 AM, "Ibrahim M. Ghazal" wrote:

    the uncertainty of whether it is a json array or single object
    How is the JSON generated? Do you have control over it? I think it
    might be easier to make the JSON consistent.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • DisposaBoy at Aug 8, 2013 at 6:25 am
    What did you try and in what way did you fail

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Martin Schnabel at Aug 6, 2013 at 8:27 am

    On 08/05/2013 10:47 PM, Tobias Contreras wrote:
    New to GO,

    I am reading in a json from post, I tried desperately to try to use a
    dynamic map to interface to deal with the uncertainty of whether it is a
    json array or single object, to no luck, so trying it the "Right" way
    and building a struct of the expected element(s) but how do I know if to
    json.Unmarshal to a struct or an array of structs?
    Or do I try unmarshal both and what doesn't err, keep?

    func PostStatement(w http.ResponseWriter, r *http.Request){

    body, err := ioutil.ReadAll(r.Body)
    var statements []Statement //but what if json is a single statement
    err = json.Unmarshal([]byte(body), &statements)
    ...
    as Ibrahim said, it is better to use either array or struct consistently.
    but if you have no control over that you can simply check whether the
    first non-whitespace byte is a square bracket. like this:

          http://play.golang.org/p/dD98vrWF7a

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Tobias Contreras at Aug 13, 2013 at 1:46 pm
    Thank you, that was my other choice on how to deal with this, thank you for
    this code, I wasn't sure how to go about this yet.
    On Tuesday, August 6, 2013 2:26:56 AM UTC-6, mb0 wrote:
    On 08/05/2013 10:47 PM, Tobias Contreras wrote:
    New to GO,

    I am reading in a json from post, I tried desperately to try to use a
    dynamic map to interface to deal with the uncertainty of whether it is a
    json array or single object, to no luck, so trying it the "Right" way
    and building a struct of the expected element(s) but how do I know if to
    json.Unmarshal to a struct or an array of structs?
    Or do I try unmarshal both and what doesn't err, keep?

    func PostStatement(w http.ResponseWriter, r *http.Request){

    body, err := ioutil.ReadAll(r.Body)
    var statements []Statement //but what if json is a single statement
    err = json.Unmarshal([]byte(body), &statements)
    ...
    as Ibrahim said, it is better to use either array or struct consistently.
    but if you have no control over that you can simply check whether the
    first non-whitespace byte is a square bracket. like this:

    http://play.golang.org/p/dD98vrWF7a
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • DisposaBoy at Aug 13, 2013 at 5:07 pm
    I'm guessing the solution came from someone who replied to you directly as opposed to the list. This interface won't show me the quoted text... But if do you mind forwarding or quoting it if it isn't already quoted so other readers may see as well? thanks.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Martin Schnabel at Aug 13, 2013 at 6:45 pm
    https://groups.google.com/forum/#!msg/golang-nuts/rKVn8coJMlQ/QkIhuH65FD8J

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Tobias Contreras at Aug 13, 2013 at 9:34 pm
    Thanks, thats the comment, it's a bummer there isn't a more elegant
    solution to trying to json decode to different structs until one fits
    without error
    I'll be trying the character matching solution out tonight
    On Tuesday, August 13, 2013 12:45:29 PM UTC-6, mb0 wrote:

    https://groups.google.com/forum/#!msg/golang-nuts/rKVn8coJMlQ/QkIhuH65FD8J
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Tobias Contreras at Aug 13, 2013 at 11:27 pm
    Hi, figured it out with mB0 help, here is some similar code I
    used, http://play.golang.org/p/-3nzhqzS1u

    is there some way to re-read a reader/buffer instead of getting an EOF?
    On Tuesday, August 13, 2013 3:34:40 PM UTC-6, Tobias Contreras wrote:

    Thanks, thats the comment, it's a bummer there isn't a more elegant
    solution to trying to json decode to different structs until one fits
    without error
    I'll be trying the character matching solution out tonight
    On Tuesday, August 13, 2013 12:45:29 PM UTC-6, mb0 wrote:

    https://groups.google.com/forum/#!msg/golang-nuts/rKVn8coJMlQ/QkIhuH65FD8J
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Tamás Gulácsi at Aug 14, 2013 at 4:36 am
    Nope, you have to store the already read parts. This can be made as space efficient as possible by using an io.TeeReader with a bytes.Buffer and then an io.MultiReader, but if it fits in memory, than use ioutil.ReadAll and bytes.NewReader.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Vasiliy Lozovoy at Aug 6, 2013 at 12:23 pm
    You can unmarshal to interface{} value and get either a map or a slice
    of maps: http://play.golang.org/p/vNQUH4zj9v

    On Mon, 5 Aug 2013 13:47:44 -0700 (PDT)
    Tobias Contreras wrote:
    New to GO,

    I am reading in a json from post, I tried desperately to try to use a
    dynamic map to interface to deal with the uncertainty of whether it
    is a json array or single object, to no luck, so trying it the
    "Right" way and building a struct of the expected element(s) but how
    do I know if to json.Unmarshal to a struct or an array of structs?
    Or do I try unmarshal both and what doesn't err, keep?

    func PostStatement(w http.ResponseWriter, r *http.Request){

    body, err := ioutil.ReadAll(r.Body)
    var statements []Statement //but what if json is a single statement
    err = json.Unmarshal([]byte(body), &statements)
    ...
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Oleku Konko at Aug 6, 2013 at 1:47 pm
    I still strongly believe the way `go` handles json can be improved raised a
    question on this already :

    https://groups.google.com/forum/#!topic/golang-nuts/pxHSWrx6h4A



    On Tuesday, August 6, 2013 7:56:20 AM UTC+1, Vasiliy wrote:

    You can unmarshal to interface{} value and get either a map or a slice
    of maps: http://play.golang.org/p/vNQUH4zj9v

    On Mon, 5 Aug 2013 13:47:44 -0700 (PDT)
    Tobias Contreras <tobi...@gmail.com <javascript:>> wrote:
    New to GO,

    I am reading in a json from post, I tried desperately to try to use a
    dynamic map to interface to deal with the uncertainty of whether it
    is a json array or single object, to no luck, so trying it the
    "Right" way and building a struct of the expected element(s) but how
    do I know if to json.Unmarshal to a struct or an array of structs?
    Or do I try unmarshal both and what doesn't err, keep?

    func PostStatement(w http.ResponseWriter, r *http.Request){

    body, err := ioutil.ReadAll(r.Body)
    var statements []Statement //but what if json is a single statement
    err = json.Unmarshal([]byte(body), &statements)
    ...
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Dustin Sallings at Aug 6, 2013 at 5:23 pm
    Tobias Contreras <tobias.ca@gmail.com>
    writes:
    body, err := ioutil.ReadAll(r.Body)
       If you'll please forgive me, I'm on a bit of a crusade against
    ioutil.ReadAll -- I see it used all over the place unnecessarily and
    often in harmful ways.

       It requires a contiguous allocation of memory to hold whatever happens
    to be on the other end of that io.Reader.

       In *most* cases where I see people doing this, they want to do
    something incremental with it when they're done (such as save it to a
    file or run it through a parser).

       e.g. here, you are just trying to parse JSON, which you can do just fine
    with an io.Reader.
    var statements []Statement //but what if json is a single statement
    err = json.Unmarshal([]byte(body), &statements)
       Instead of this, you can just use a json.Decoder and parse on the fly:

         d := json.NewDecoder(r.Body)
         err = d.Decode(&statements)

       The same pattern works the same way with an infinite multi-object JSON
    stream or a single small object.


       In your particular case, it may not make a practical difference, but,
    IMO, it's good to think in terms of Readers and Writers vs. large slabs
    of contiguous memory.

    --
    dustin

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Tobias Contreras at Aug 13, 2013 at 1:44 pm
    That's really good to know, do you have a golang blog?
    On Tuesday, August 6, 2013 11:22:54 AM UTC-6, Dustin wrote:

    Tobias Contreras <tobi...@gmail.com <javascript:>>
    writes:
    body, err := ioutil.ReadAll(r.Body)
    If you'll please forgive me, I'm on a bit of a crusade against
    ioutil.ReadAll -- I see it used all over the place unnecessarily and
    often in harmful ways.

    It requires a contiguous allocation of memory to hold whatever happens
    to be on the other end of that io.Reader.

    In *most* cases where I see people doing this, they want to do
    something incremental with it when they're done (such as save it to a
    file or run it through a parser).

    e.g. here, you are just trying to parse JSON, which you can do just fine
    with an io.Reader.
    var statements []Statement //but what if json is a single statement
    err = json.Unmarshal([]byte(body), &statements)
    Instead of this, you can just use a json.Decoder and parse on the fly:

    d := json.NewDecoder(r.Body)
    err = d.Decode(&statements)

    The same pattern works the same way with an infinite multi-object JSON
    stream or a single small object.


    In your particular case, it may not make a practical difference, but,
    IMO, it's good to think in terms of Readers and Writers vs. large slabs
    of contiguous memory.

    --
    dustin
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Tobias Contreras at Aug 13, 2013 at 1:39 pm
    I tried something like, but the buffer remains at EOF

    decoder := json.NewDecoder(r.Body)

    var statements []Statement
    err := decoder.Decode(&statements)
    if err != nil {
      var statement Statement
              err2 := decoder.Decode(&statement)
              if err2 != nil {
                   //but err2 just errors with EOF, and I can't find a way to
    re-read body, or reset buffer
                   return
              }
    }

    I also tried reading it into an interface{} but then I couldn't figure how
    to convert to the statement object or array

    So last but not least going to try checking for first char square bracket
    On Monday, August 5, 2013 2:47:44 PM UTC-6, Tobias Contreras wrote:

    New to GO,

    I am reading in a json from post, I tried desperately to try to use a
    dynamic map to interface to deal with the uncertainty of whether it is a
    json array or single object, to no luck, so trying it the "Right" way and
    building a struct of the expected element(s) but how do I know if to
    json.Unmarshal to a struct or an array of structs?
    Or do I try unmarshal both and what doesn't err, keep?

    func PostStatement(w http.ResponseWriter, r *http.Request){

    body, err := ioutil.ReadAll(r.Body)
    var statements []Statement //but what if json is a single statement
    err = json.Unmarshal([]byte(body), &statements)
    ...
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedAug 6, '13 at 5:09a
activeAug 14, '13 at 4:36a
posts16
users8
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase