Unmarshaling JSON file

202 views
Skip to first unread message

deepak

unread,
Dec 11, 2013, 8:32:42 AM12/11/13
to golan...@googlegroups.com
Hi all,

I am new to go programming, I am trying to parse the following JSON file

{
  "movies": [
    {
      "name": "Inception",
      "rating": 8.8,
      "genres": [
        "Action",
        "Adventure",
        "Sci-Fi"
      ]
    },
    {
      "name": "Godfather",
      "rating": 9.2,
      "genres": [
        "Crime",
        "Drama"
      ]
    },
    ...
  ]
}


I created a movie struct to represent each movie

type Movie struct { name string rating float64 genres []string }

I have parsed the file like this
 
var raw interface{} data, _ := ioutil.ReadFile(filename) _ = json.Unmarshal(data, &raw)

so far everything is fine. Please tell me how access each movie and iterate using a loop. When I try to do that I am getting some type related error.

Jan Mercl

unread,
Dec 11, 2013, 8:39:05 AM12/11/13
to deepak, golang-nuts
On Wed, Dec 11, 2013 at 2:32 PM, deepak <deepak...@gmail.com> wrote:
> I created a movie struct to represent each movie
>
> type Movie struct {
> name string
> rating float64
> genres []string
> }
>
> I have parsed the file like this
>
> var raw interface{}
> data, _ := ioutil.ReadFile(filename)
> _ = json.Unmarshal(data, &raw)
>
> so far everything is fine. Please tell me how access each movie and iterate
> using a loop. When I try to do that I am getting some type related error.

Please publish some compilable code, for example at play.golang.org.
That'll help to help you.

-j

Matthew Kane

unread,
Dec 11, 2013, 8:44:48 AM12/11/13
to deepak, golang-nuts
You should not ignore errors, although in this case you wouldn't get
any unless the file was missing.

First, in order to unmarshal to an actual array of Movie structs,
"raw" needs to be of that type. In order for the json package to be
able to populate fields in the structs, they must be exported. e.g.
http://play.golang.org/p/Ij0zb1EUIj
> --
> 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...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.



--
matt kane
twitter: the_real_mkb / nynexrepublic
http://hydrogenproject.com

deepak

unread,
Dec 11, 2013, 9:15:54 AM12/11/13
to golan...@googlegroups.com, deepak
here is the code http://play.golang.org/p/o6XVdixZNf I am stuck at the comment line

Sebastien Binet

unread,
Dec 11, 2013, 9:28:59 AM12/11/13
to deepak, golang-nuts

egon

unread,
Dec 11, 2013, 9:30:41 AM12/11/13
to golan...@googlegroups.com, deepak
The one-line comments start with "//" in go.

Also the outermost object is  { "movies": [...] }... the structure you marshal into must match that... it can't go and magically find the correct inner element. e.g. "type Outer struct { Movies []Movie }"

I suggest starting with http://tour.golang.org/ and https://gobyexample.com/ it'll teach most of the things you need to know...

+ egon

deepak kumar

unread,
Dec 11, 2013, 9:35:43 AM12/11/13
to egon, golan...@googlegroups.com
oh great thanks guys for the quick response

@Sebastien that works thanks, But I didn't get the `json:"xyz"` part. Can suggest some document which explains it, or can u explain it?

@egon, my bad, I am coming from ruby background, so accidentally commented like that.  Thanks for the links 
--
Thanks & Regards
Deepak Kumar


Jan Mercl

unread,
Dec 11, 2013, 9:39:36 AM12/11/13
to deepak, golang-nuts
On Wed, Dec 11, 2013 at 3:15 PM, deepak <deepak...@gmail.com> wrote:
> here is the code http://play.golang.org/p/o6XVdixZNf I am stuck at the
> comment line

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

The only important change required was to export the field (make its
name upper case).

-j

deepak kumar

unread,
Dec 11, 2013, 10:24:09 AM12/11/13
to Jan Mercl, golang-nuts
Oh thanks @Jan that was what I was trying to do, so making field uppercase is that important ?

Caleb Spare

unread,
Dec 11, 2013, 10:45:01 AM12/11/13
to golan...@googlegroups.com, Jan Mercl
Yes, starting with uppercase is how a struct field is exported.

The json package documentation mentions that it works on exported fields and also discusses struct tags:


-Caleb

deepak kumar

unread,
Dec 11, 2013, 10:49:26 AM12/11/13
to Caleb Spare, Jan Mercl, golan...@googlegroups.com

OK great thanks Caleb for the info

--
You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/u3BjQZsyVpg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages