FAQ
Receiving a json string, I have no control over, has an element that is an object or array of objects. How do I read the json to convert the element from object to array of the single object so I can set the json to a strict.

Thought about regex replace or Marshall to interface then try to reset element as array of interface then hopefully assert to struct . ( no idea if any of that is possible yet, still new)

Thanks for any insight.

--
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

  • Athiwat Chunlakhan at Sep 29, 2013 at 3:21 pm
    Can you post an example of your JSON and what the output should look like?

    If I understand you correctly, then the code below should help.

    For now you can use something like

    type Location struct {
    Type string `json:"myOtherName"`
    Coordinates [2]float64 `json:"DoesNotHaveToBeCoordinates"`
    }

    This will automatically get the field myOtherName into Type and
    DoesNotHaveToBeCoordinates into Coordinates without you need to pre-process
    anything.

    Or in case the most outter is an object that contains an array of elements
    that you need in the struct. You can do something like

    type Location struct {
    Type string `json:"myOtherName"`
    Coordinates [2]float64 `json:"DoesNotHaveToBeCoordinates"`
    }
    type Container struct {
             ListOfLoc []Location `json:"ListOfLocation"`
    }

    This will allow to to have separate Location slice and you could throw the
    Container away afterward.
    On Sunday, September 29, 2013 9:58:37 PM UTC+7, Tobias Contreras wrote:

    Receiving a json string, I have no control over, has an element that is an
    object or array of objects. How do I read the json to convert the element
    from object to array of the single object so I can set the json to a strict.

    Thought about regex replace or Marshall to interface then try to reset
    element as array of interface then hopefully assert to struct . ( no idea
    if any of that is possible yet, still new)

    Thanks for any insight.
    --
    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 Sep 29, 2013 at 4:32 pm
    here is a simple example (real one is march bigger and nested)
    *example has error but i dont know why

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

    On Sunday, September 29, 2013 9:21:15 AM UTC-6, Athiwat Chunlakhan wrote:

    Can you post an example of your JSON and what the output should look like?

    If I understand you correctly, then the code below should help.

    For now you can use something like

    type Location struct {
    Type string `json:"myOtherName"`
    Coordinates [2]float64 `json:"DoesNotHaveToBeCoordinates"`
    }

    This will automatically get the field myOtherName into Type and
    DoesNotHaveToBeCoordinates into Coordinates without you need to pre-process
    anything.

    Or in case the most outter is an object that contains an array of elements
    that you need in the struct. You can do something like

    type Location struct {
    Type string `json:"myOtherName"`
    Coordinates [2]float64 `json:"DoesNotHaveToBeCoordinates"`
    }
    type Container struct {
    ListOfLoc []Location `json:"ListOfLocation"`
    }

    This will allow to to have separate Location slice and you could throw the
    Container away afterward.
    On Sunday, September 29, 2013 9:58:37 PM UTC+7, Tobias Contreras wrote:

    Receiving a json string, I have no control over, has an element that is
    an object or array of objects. How do I read the json to convert the
    element from object to array of the single object so I can set the json to
    a strict.

    Thought about regex replace or Marshall to interface then try to reset
    element as array of interface then hopefully assert to struct . ( no idea
    if any of that is possible yet, still new)

    Thanks for any insight.
    --
    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 Sep 29, 2013 at 4:44 pm
    oh ,error, I guess because its is mapping to interface{} i need to use
    reflection class to reference key/values
    On Sunday, September 29, 2013 10:32:53 AM UTC-6, Tobias Contreras wrote:

    here is a simple example (real one is march bigger and nested)
    *example has error but i dont know why

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

    On Sunday, September 29, 2013 9:21:15 AM UTC-6, Athiwat Chunlakhan wrote:

    Can you post an example of your JSON and what the output should look like?

    If I understand you correctly, then the code below should help.

    For now you can use something like

    type Location struct {
    Type string `json:"myOtherName"`
    Coordinates [2]float64 `json:"DoesNotHaveToBeCoordinates"`
    }

    This will automatically get the field myOtherName into Type and
    DoesNotHaveToBeCoordinates into Coordinates without you need to pre-process
    anything.

    Or in case the most outter is an object that contains an array of
    elements that you need in the struct. You can do something like

    type Location struct {
    Type string `json:"myOtherName"`
    Coordinates [2]float64 `json:"DoesNotHaveToBeCoordinates"`
    }
    type Container struct {
    ListOfLoc []Location `json:"ListOfLocation"`
    }

    This will allow to to have separate Location slice and you could throw
    the Container away afterward.
    On Sunday, September 29, 2013 9:58:37 PM UTC+7, Tobias Contreras wrote:

    Receiving a json string, I have no control over, has an element that is
    an object or array of objects. How do I read the json to convert the
    element from object to array of the single object so I can set the json to
    a strict.

    Thought about regex replace or Marshall to interface then try to reset
    element as array of interface then hopefully assert to struct . ( no idea
    if any of that is possible yet, still new)

    Thanks for any insight.
    --
    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 Sep 29, 2013 at 5:25 pm
    Ya, even with reflection, still no idea how to solve this problem
    On Sunday, September 29, 2013 10:44:05 AM UTC-6, Tobias Contreras wrote:

    oh ,error, I guess because its is mapping to interface{} i need to use
    reflection class to reference key/values
    On Sunday, September 29, 2013 10:32:53 AM UTC-6, Tobias Contreras wrote:

    here is a simple example (real one is march bigger and nested)
    *example has error but i dont know why

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

    On Sunday, September 29, 2013 9:21:15 AM UTC-6, Athiwat Chunlakhan wrote:

    Can you post an example of your JSON and what the output should look
    like?

    If I understand you correctly, then the code below should help.

    For now you can use something like

    type Location struct {
    Type string `json:"myOtherName"`
    Coordinates [2]float64 `json:"DoesNotHaveToBeCoordinates"`
    }

    This will automatically get the field myOtherName into Type and
    DoesNotHaveToBeCoordinates into Coordinates without you need to pre-process
    anything.

    Or in case the most outter is an object that contains an array of
    elements that you need in the struct. You can do something like

    type Location struct {
    Type string `json:"myOtherName"`
    Coordinates [2]float64 `json:"DoesNotHaveToBeCoordinates"`
    }
    type Container struct {
    ListOfLoc []Location `json:"ListOfLocation"`
    }

    This will allow to to have separate Location slice and you could throw
    the Container away afterward.
    On Sunday, September 29, 2013 9:58:37 PM UTC+7, Tobias Contreras wrote:

    Receiving a json string, I have no control over, has an element that is
    an object or array of objects. How do I read the json to convert the
    element from object to array of the single object so I can set the json to
    a strict.

    Thought about regex replace or Marshall to interface then try to reset
    element as array of interface then hopefully assert to struct . ( no idea
    if any of that is possible yet, still new)

    Thanks for any insight.
    --
    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.
  • Philippe Quesnel at Sep 29, 2013 at 6:26 pm
    Since you don't parse into a known type (e.g. Actor), the parsed JSON gives
    you a map[string]interface{},
    so to access the members you need to convert to map[string]interface{} and
    then access the map:
    http://play.golang.org/p/e3551KxNC3

    of course you don't have type safety here,
    you would need to recursively convert each element by hand, (should check
    for correct conversion!)
    i.e. the "account" is itself a map[string]interface{} :
      accountMap := actorMap["account"].(map[string]interface{})

    On Sunday, 29 September 2013 13:24:58 UTC-4, Tobias Contreras wrote:

    Ya, even with reflection, still no idea how to solve this problem
    On Sunday, September 29, 2013 10:44:05 AM UTC-6, Tobias Contreras wrote:

    oh ,error, I guess because its is mapping to interface{} i need to use
    reflection class to reference key/values
    On Sunday, September 29, 2013 10:32:53 AM UTC-6, Tobias Contreras wrote:

    here is a simple example (real one is march bigger and nested)
    *example has error but i dont know why

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

    On Sunday, September 29, 2013 9:21:15 AM UTC-6, Athiwat Chunlakhan wrote:

    Can you post an example of your JSON and what the output should look
    like?

    If I understand you correctly, then the code below should help.

    For now you can use something like

    type Location struct {
    Type string `json:"myOtherName"`
    Coordinates [2]float64 `json:"DoesNotHaveToBeCoordinates"`
    }

    This will automatically get the field myOtherName into Type and
    DoesNotHaveToBeCoordinates into Coordinates without you need to pre-process
    anything.

    Or in case the most outter is an object that contains an array of
    elements that you need in the struct. You can do something like

    type Location struct {
    Type string `json:"myOtherName"`
    Coordinates [2]float64 `json:"DoesNotHaveToBeCoordinates"`
    }
    type Container struct {
    ListOfLoc []Location `json:"ListOfLocation"`
    }

    This will allow to to have separate Location slice and you could throw
    the Container away afterward.
    On Sunday, September 29, 2013 9:58:37 PM UTC+7, Tobias Contreras wrote:

    Receiving a json string, I have no control over, has an element that
    is an object or array of objects. How do I read the json to convert the
    element from object to array of the single object so I can set the json to
    a strict.

    Thought about regex replace or Marshall to interface then try to reset
    element as array of interface then hopefully assert to struct . ( no idea
    if any of that is possible yet, still new)

    Thanks for any insight.
    --
    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.
  • Philippe Quesnel at Sep 29, 2013 at 6:27 pm
    Another way to do this, would be to also have a definition for the struct
    where you have a single Account :
    type ActorSingleAccount struct {
    Name string `json:"name,omitempty"`
    Account Account `json:"account,omitempty"`
    }
    type Actor struct {
    Name string `json:"name,omitempty"`
    Accounts []*Account `json:"account,omitempty"`
    }
    then you could try to parse an Actor, if it fails, try with an
    ActorSingleAccount.
    http://play.golang.org/p/vMjPY-b6HQ

    You still have to copy from ActorSingleAccount to Actor though,
    but at least you get the right types w/o having to manually convert to
    proper type and check that it worked

    I don't know if this can apply to your real world situation ??
    On Sunday, 29 September 2013 13:24:58 UTC-4, Tobias Contreras wrote:

    Ya, even with reflection, still no idea how to solve this problem
    On Sunday, September 29, 2013 10:44:05 AM UTC-6, Tobias Contreras wrote:

    oh ,error, I guess because its is mapping to interface{} i need to use
    reflection class to reference key/values
    On Sunday, September 29, 2013 10:32:53 AM UTC-6, Tobias Contreras wrote:

    here is a simple example (real one is march bigger and nested)
    *example has error but i dont know why

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

    On Sunday, September 29, 2013 9:21:15 AM UTC-6, Athiwat Chunlakhan wrote:

    Can you post an example of your JSON and what the output should look
    like?

    If I understand you correctly, then the code below should help.

    For now you can use something like

    type Location struct {
    Type string `json:"myOtherName"`
    Coordinates [2]float64 `json:"DoesNotHaveToBeCoordinates"`
    }

    This will automatically get the field myOtherName into Type and
    DoesNotHaveToBeCoordinates into Coordinates without you need to pre-process
    anything.

    Or in case the most outter is an object that contains an array of
    elements that you need in the struct. You can do something like

    type Location struct {
    Type string `json:"myOtherName"`
    Coordinates [2]float64 `json:"DoesNotHaveToBeCoordinates"`
    }
    type Container struct {
    ListOfLoc []Location `json:"ListOfLocation"`
    }

    This will allow to to have separate Location slice and you could throw
    the Container away afterward.
    On Sunday, September 29, 2013 9:58:37 PM UTC+7, Tobias Contreras wrote:

    Receiving a json string, I have no control over, has an element that
    is an object or array of objects. How do I read the json to convert the
    element from object to array of the single object so I can set the json to
    a strict.

    Thought about regex replace or Marshall to interface then try to reset
    element as array of interface then hopefully assert to struct . ( no idea
    if any of that is possible yet, still new)

    Thanks for any insight.
    --
    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 Sep 29, 2013 at 10:11 pm
    Well Think I got a solution,

    take json, convert to map interface{}, check if element is object or array,
    wrap object as array, convert back to string (wish there was a way to skip
    this step), convert string to correct struct.

    http://play.golang.org/p/iAHwWBjXhd
    On Sunday, September 29, 2013 8:58:37 AM UTC-6, Tobias Contreras wrote:

    Receiving a json string, I have no control over, has an element that is an
    object or array of objects. How do I read the json to convert the element
    from object to array of the single object so I can set the json to a strict.

    Thought about regex replace or Marshall to interface then try to reset
    element as array of interface then hopefully assert to struct . ( no idea
    if any of that is possible yet, still new)

    Thanks for any insight.
    --
    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
postedSep 29, '13 at 2:58p
activeSep 29, '13 at 10:11p
posts8
users3
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase