2

So I am trying to parse a json into some structs and that works ok with the following:

type train struct {
 ID     string  `json:"id"`
 Price  float64 `json:"price,string"`
 Distance float64 `json:"Distance,string"`
}

type Station struct {
 ID       int64  `json:"id,string"`
 arrTrain []train`json:"arr"`
 depTrain []train`json:"dep"`
}

The problem, however, is that I would like to easily be able to reference the items in arrTrain and depTrain using their ID, so I think I need to change the Station struct to have arrTrain and depTrain as maps with the ID as the key. Is this possible when parsing the json or does it have to be 'post-processed'?

EDIT: As stated in one of the comments, unfortunately my json is in the following form:

{
  "id":1, 
  "arr": [
     {"id":"one","price":"$10.1","Distance":"100km"},
     {...}
  ],
  "dep":[
    {"id":"one","price":"$10.1","Distance":"100km"},
    {...}
  ]
}

In other words the ID is not on the outside of the json object and arrTrain is list.

2
  • 1
    I tried some of the answers below and it seems that maps formed within structs are not recognized/lost by json.Marshal - playground Commented Mar 16, 2017 at 1:35
  • 1
    Make those fields exportable play.golang.org/p/x8TOcjcj7T Commented Mar 16, 2017 at 3:47

2 Answers 2

2

Yes, you can define station like this:

type Station struct {
    ID       int64            `json:"id,string"`
    arrTrain map[string]train `json:"arr"`
    depTrain map[string]train `json:"dep"`
}

And your JSON should like this

{
  "id":1, 
  "arr": {
    "one":{"id":"one","price":"$10.1","Distance":"100km"},
    "two":...
  },
  "dep":{
    "one":{"id":"one","price":"$10.1","Distance":"100km"},
    "two":...
  }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Ah, I see, thank you. Unfortunately, my json has the form "arr":[{},{}], i.e. the key is not outside the individual item.
@RexFuzzle Base on this information, I think the best way is defining UnmarshalJSON function for Station struct.
@netdigger see my answer but make the field names capitalized.
1

Edit: Yes you can. (an earlier version of this answer stated keys can only be strings, but as of 1.7 this is not true)

You can see this in action with this playground

Also as conner points out in the comments your field names will have to be exportable for encoding/json to work

type Station struct {
    ID       int64            `json:"id,string"`
    ArrTrain map[string]train `json:"arr"`
    DepTrain map[string]train `json:"dep"`
}

1 Comment

since 1.7 encoding/json supports map keys with integer types or keys that implement the encoding.TextMarhsaler/TextUnmarshaler interface. release notes, commit

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.