unmarshall anonymous json field to “named” fieldFlattening marshalled JSON structs with anonymous members in GoJSON Unmarshall not working as expected with structsCustom JSON Unmarshalling for string-encoded numberJSON decode unknown objectAdding Arbitrary fields to json output of an unknown structJSON unmarshal integer field into a stringJSON Marshaling of composite structs which both implement MarshalJSON()Concatenate two values from a JSON object into a map[string]bool in GoAssign additional field when unmarshalling JSON object to GO structGo getting failed field from UnmarshalTypeError

what to say when a company asks you why someone (a friend) who was fired left?

In a script how can I signal who's winning the argument?

Found more old paper shares from broken up companies

Why are MEMS in QFN packages?

What was the rationale behind 36 bit computer architectures?

What is an Eternal Word™?

What's the 1 inch size square knob sticking out of wall?

Why are angular mometum and angular velocity not necessarily parallel, but linear momentum and linear velocity are always parallel?

Aging LEDs - does their output drop after turn-on?

how to add 1 milliseconds on a datetime string?

Can GPL and BSD licensed applications be used for government work?

What is the best word describing the nature of expiring in a short amount of time, connoting "losing public attention"?

What is the relationship between the theme songs in Sherlock Holmes (2009 movie) and Sherlock (BBC series)?

Bug in Lualatex: not printing characters from calculation

Can a character with a low Intelligence score take the Ritual Caster feat and choose the Wizard class?

The seven story archetypes. Are they truly all of them?

Why is a dedicated QA team member necessary?

Raw curve25519 public key points

How to Trust a Self-Signed Certificate

Is there a way to shorten this while condition?

Idioms: Should it be " the internet is a seemingly infinite well of information" or "the internet is a seemingly infinite wealth of information"

Can't understand how static works exactly

Alternative methods for solving a system of one linear one non linear simultaneous equations

Are irregular workouts through the day effective?



unmarshall anonymous json field to “named” field


Flattening marshalled JSON structs with anonymous members in GoJSON Unmarshall not working as expected with structsCustom JSON Unmarshalling for string-encoded numberJSON decode unknown objectAdding Arbitrary fields to json output of an unknown structJSON unmarshal integer field into a stringJSON Marshaling of composite structs which both implement MarshalJSON()Concatenate two values from a JSON object into a map[string]bool in GoAssign additional field when unmarshalling JSON object to GO structGo getting failed field from UnmarshalTypeError






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















Say I have the following json




"unknown":
"knownArray": [
"property": "somevalue", "otherproperty": false
],
"otherKnownArray": [""]




And I have the following structs to represent this data



type Model struct 
ObjectName string
KnownArray []KnownType `json:"knownArray"`
OtherKnownArray []string `json:"otherKnownArray"`


type KnownType struct
Property string `json:"property1"`
Otherproperty bool `json:"otherproperty"`



doing



var model Model

json.Unmarshal(content, &model)


Does not deserialize any of the json unfortunately.



How do I deserialize to my Model correctly?
How do I deserialize the json so that ObjectName = "unknown"?



Im not quite understanding the internals of encoding/json when it comes to anonymous json fields.



Ive also tried wrapping Model in a third "outer" Model, but still does not work with the anonymous json field.










share|improve this question



















  • 2





    Use map[string]*Model, then range over the map and use the key to set the ObjectName.

    – mkopriva
    Mar 26 at 15:23


















0















Say I have the following json




"unknown":
"knownArray": [
"property": "somevalue", "otherproperty": false
],
"otherKnownArray": [""]




And I have the following structs to represent this data



type Model struct 
ObjectName string
KnownArray []KnownType `json:"knownArray"`
OtherKnownArray []string `json:"otherKnownArray"`


type KnownType struct
Property string `json:"property1"`
Otherproperty bool `json:"otherproperty"`



doing



var model Model

json.Unmarshal(content, &model)


Does not deserialize any of the json unfortunately.



How do I deserialize to my Model correctly?
How do I deserialize the json so that ObjectName = "unknown"?



Im not quite understanding the internals of encoding/json when it comes to anonymous json fields.



Ive also tried wrapping Model in a third "outer" Model, but still does not work with the anonymous json field.










share|improve this question



















  • 2





    Use map[string]*Model, then range over the map and use the key to set the ObjectName.

    – mkopriva
    Mar 26 at 15:23














0












0








0








Say I have the following json




"unknown":
"knownArray": [
"property": "somevalue", "otherproperty": false
],
"otherKnownArray": [""]




And I have the following structs to represent this data



type Model struct 
ObjectName string
KnownArray []KnownType `json:"knownArray"`
OtherKnownArray []string `json:"otherKnownArray"`


type KnownType struct
Property string `json:"property1"`
Otherproperty bool `json:"otherproperty"`



doing



var model Model

json.Unmarshal(content, &model)


Does not deserialize any of the json unfortunately.



How do I deserialize to my Model correctly?
How do I deserialize the json so that ObjectName = "unknown"?



Im not quite understanding the internals of encoding/json when it comes to anonymous json fields.



Ive also tried wrapping Model in a third "outer" Model, but still does not work with the anonymous json field.










share|improve this question
















Say I have the following json




"unknown":
"knownArray": [
"property": "somevalue", "otherproperty": false
],
"otherKnownArray": [""]




And I have the following structs to represent this data



type Model struct 
ObjectName string
KnownArray []KnownType `json:"knownArray"`
OtherKnownArray []string `json:"otherKnownArray"`


type KnownType struct
Property string `json:"property1"`
Otherproperty bool `json:"otherproperty"`



doing



var model Model

json.Unmarshal(content, &model)


Does not deserialize any of the json unfortunately.



How do I deserialize to my Model correctly?
How do I deserialize the json so that ObjectName = "unknown"?



Im not quite understanding the internals of encoding/json when it comes to anonymous json fields.



Ive also tried wrapping Model in a third "outer" Model, but still does not work with the anonymous json field.







go






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 26 at 15:40







VisualBean

















asked Mar 26 at 15:19









VisualBeanVisualBean

3,2412 gold badges19 silver badges47 bronze badges




3,2412 gold badges19 silver badges47 bronze badges







  • 2





    Use map[string]*Model, then range over the map and use the key to set the ObjectName.

    – mkopriva
    Mar 26 at 15:23













  • 2





    Use map[string]*Model, then range over the map and use the key to set the ObjectName.

    – mkopriva
    Mar 26 at 15:23








2




2





Use map[string]*Model, then range over the map and use the key to set the ObjectName.

– mkopriva
Mar 26 at 15:23






Use map[string]*Model, then range over the map and use the key to set the ObjectName.

– mkopriva
Mar 26 at 15:23













1 Answer
1






active

oldest

votes


















2














can use map[string]Model to encode. https://play.golang.org/p/QWXQZFjBgRB



package main

import (
"fmt"
"encoding/json"
)

type Model struct
ObjectName string
KnownArray []KnownType `json:"knownArray"`
OtherKnownArray []string `json:"otherKnownArray"`


type KnownType struct
Property string `json:"property"`
Otherproperty bool `json:"otherproperty"`


func main()
jsonstring := `
"unknown":
"knownArray": [
"property": "somevalue", "otherproperty": false
],
"otherKnownArray": [""]

`
a := make(map[string]Model)
json.Unmarshal([]byte(jsonstring), &a)
var m Model
for k, v := range(a)
m = v
m.ObjectName = k
break

fmt.Println(m.ObjectName, m.KnownArray, m.OtherKnownArray)






share|improve this answer

























  • This answers half the question, which is awesome! im am 100% further now. But what about the property "ObjectName"? or do I just assign it after the fact?

    – VisualBean
    Mar 26 at 15:40











  • @VisualBean, I updated the answer. Yes, you just assign it

    – Pan Ke
    Mar 26 at 15:44











  • Ooh because its the key.. ofcourse.. Thank you!

    – VisualBean
    Mar 26 at 15:45










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55360658%2funmarshall-anonymous-json-field-to-named-field%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














can use map[string]Model to encode. https://play.golang.org/p/QWXQZFjBgRB



package main

import (
"fmt"
"encoding/json"
)

type Model struct
ObjectName string
KnownArray []KnownType `json:"knownArray"`
OtherKnownArray []string `json:"otherKnownArray"`


type KnownType struct
Property string `json:"property"`
Otherproperty bool `json:"otherproperty"`


func main()
jsonstring := `
"unknown":
"knownArray": [
"property": "somevalue", "otherproperty": false
],
"otherKnownArray": [""]

`
a := make(map[string]Model)
json.Unmarshal([]byte(jsonstring), &a)
var m Model
for k, v := range(a)
m = v
m.ObjectName = k
break

fmt.Println(m.ObjectName, m.KnownArray, m.OtherKnownArray)






share|improve this answer

























  • This answers half the question, which is awesome! im am 100% further now. But what about the property "ObjectName"? or do I just assign it after the fact?

    – VisualBean
    Mar 26 at 15:40











  • @VisualBean, I updated the answer. Yes, you just assign it

    – Pan Ke
    Mar 26 at 15:44











  • Ooh because its the key.. ofcourse.. Thank you!

    – VisualBean
    Mar 26 at 15:45















2














can use map[string]Model to encode. https://play.golang.org/p/QWXQZFjBgRB



package main

import (
"fmt"
"encoding/json"
)

type Model struct
ObjectName string
KnownArray []KnownType `json:"knownArray"`
OtherKnownArray []string `json:"otherKnownArray"`


type KnownType struct
Property string `json:"property"`
Otherproperty bool `json:"otherproperty"`


func main()
jsonstring := `
"unknown":
"knownArray": [
"property": "somevalue", "otherproperty": false
],
"otherKnownArray": [""]

`
a := make(map[string]Model)
json.Unmarshal([]byte(jsonstring), &a)
var m Model
for k, v := range(a)
m = v
m.ObjectName = k
break

fmt.Println(m.ObjectName, m.KnownArray, m.OtherKnownArray)






share|improve this answer

























  • This answers half the question, which is awesome! im am 100% further now. But what about the property "ObjectName"? or do I just assign it after the fact?

    – VisualBean
    Mar 26 at 15:40











  • @VisualBean, I updated the answer. Yes, you just assign it

    – Pan Ke
    Mar 26 at 15:44











  • Ooh because its the key.. ofcourse.. Thank you!

    – VisualBean
    Mar 26 at 15:45













2












2








2







can use map[string]Model to encode. https://play.golang.org/p/QWXQZFjBgRB



package main

import (
"fmt"
"encoding/json"
)

type Model struct
ObjectName string
KnownArray []KnownType `json:"knownArray"`
OtherKnownArray []string `json:"otherKnownArray"`


type KnownType struct
Property string `json:"property"`
Otherproperty bool `json:"otherproperty"`


func main()
jsonstring := `
"unknown":
"knownArray": [
"property": "somevalue", "otherproperty": false
],
"otherKnownArray": [""]

`
a := make(map[string]Model)
json.Unmarshal([]byte(jsonstring), &a)
var m Model
for k, v := range(a)
m = v
m.ObjectName = k
break

fmt.Println(m.ObjectName, m.KnownArray, m.OtherKnownArray)






share|improve this answer















can use map[string]Model to encode. https://play.golang.org/p/QWXQZFjBgRB



package main

import (
"fmt"
"encoding/json"
)

type Model struct
ObjectName string
KnownArray []KnownType `json:"knownArray"`
OtherKnownArray []string `json:"otherKnownArray"`


type KnownType struct
Property string `json:"property"`
Otherproperty bool `json:"otherproperty"`


func main()
jsonstring := `
"unknown":
"knownArray": [
"property": "somevalue", "otherproperty": false
],
"otherKnownArray": [""]

`
a := make(map[string]Model)
json.Unmarshal([]byte(jsonstring), &a)
var m Model
for k, v := range(a)
m = v
m.ObjectName = k
break

fmt.Println(m.ObjectName, m.KnownArray, m.OtherKnownArray)







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 26 at 15:44

























answered Mar 26 at 15:33









Pan KePan Ke

5497 bronze badges




5497 bronze badges












  • This answers half the question, which is awesome! im am 100% further now. But what about the property "ObjectName"? or do I just assign it after the fact?

    – VisualBean
    Mar 26 at 15:40











  • @VisualBean, I updated the answer. Yes, you just assign it

    – Pan Ke
    Mar 26 at 15:44











  • Ooh because its the key.. ofcourse.. Thank you!

    – VisualBean
    Mar 26 at 15:45

















  • This answers half the question, which is awesome! im am 100% further now. But what about the property "ObjectName"? or do I just assign it after the fact?

    – VisualBean
    Mar 26 at 15:40











  • @VisualBean, I updated the answer. Yes, you just assign it

    – Pan Ke
    Mar 26 at 15:44











  • Ooh because its the key.. ofcourse.. Thank you!

    – VisualBean
    Mar 26 at 15:45
















This answers half the question, which is awesome! im am 100% further now. But what about the property "ObjectName"? or do I just assign it after the fact?

– VisualBean
Mar 26 at 15:40





This answers half the question, which is awesome! im am 100% further now. But what about the property "ObjectName"? or do I just assign it after the fact?

– VisualBean
Mar 26 at 15:40













@VisualBean, I updated the answer. Yes, you just assign it

– Pan Ke
Mar 26 at 15:44





@VisualBean, I updated the answer. Yes, you just assign it

– Pan Ke
Mar 26 at 15:44













Ooh because its the key.. ofcourse.. Thank you!

– VisualBean
Mar 26 at 15:45





Ooh because its the key.. ofcourse.. Thank you!

– VisualBean
Mar 26 at 15:45








Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55360658%2funmarshall-anonymous-json-field-to-named-field%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript