Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

error parsing unexpected type: []interface {} #17

Open
jbuberel opened this issue Mar 15, 2015 · 4 comments
Open

error parsing unexpected type: []interface {} #17

jbuberel opened this issue Mar 15, 2015 · 4 comments

Comments

@jbuberel
Copy link

Attempting to parse the JSON generated by the Reddit.com API, and ran into the following error when trying to process the JSON from:

https://www.reddit.com/r/golang/comments/2z1frh/a_jvm_writing_in_go.json

jbuberel@golang-metrics:~/workspace $ curl https://www.reddit.com/r/golang/comments/2z1frh/a_jvm_writing_in_go.json | ~/bin/gojson --name RedditIndex --pkg reddit 
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 20696    0 20696    0     0   115k      0 --:--:-- --:--:-- --:--:--  116k
error parsing unexpected type: []interface {}

However, the JSON appears formatted correctly (it validates using http://jsonformatter.curiousconcept.com/ and http://jsonformat.com/).

The formatted JSON is:

[ { "data" : { "after" : null,
        "before" : null,
        "children" : [ { "data" : { "approved_by" : null,
                  "archived" : false,
                  "author" : "HarveyKandola",
                  "author_flair_css_class" : null,
                  "author_flair_text" : null,
                  "banned_by" : null,
                  "clicked" : false,
                  "created" : 1426385069.0,
                  "created_utc" : 1426356269.0,
                  "distinguished" : null,
                  "domain" : "github.com",
                  "downs" : 0,
                  "edited" : false,
                  "gilded" : 0,
                  "hidden" : false,
                  "id" : "2z1frh",
                  "is_self" : false,
                  "likes" : null,
                  "link_flair_css_class" : null,
                  "link_flair_text" : null,
                  "media" : null,
                  "media_embed" : {  },
                  "mod_reports" : [  ],
                  "name" : "t3_2z1frh",
                  "num_comments" : 22,
                  "num_reports" : null,
                  "over_18" : false,
                  "permalink" : "/r/golang/comments/2z1frh/a_jvm_writing_in_go/",
                  "report_reasons" : null,
                  "saved" : false,
                  "score" : 46,
                  "secure_media" : null,
                  "secure_media_embed" : {  },
                  "selftext" : "",
                  "selftext_html" : null,
                  "stickied" : false,
                  "subreddit" : "golang",
                  "subreddit_id" : "t5_2rc7j",
                  "thumbnail" : "",
                  "title" : "A JVM writing in GO",
                  "ups" : 46,
                  "upvote_ratio" : 0.88,
                  "url" : "https://github.com/zxh0/jvm.go",
                  "user_reports" : [  ],
                  "visited" : false
                },
              "kind" : "t3"
            } ],
        "modhash" : ""
      },
    "kind" : "Listing"
  },
  { "data" : { "after" : null,
        "before" : null,
        "children" : [ { "data" : { "approved_by" : null,
                  "archived" : false,
                  "author" : "rrubmo",
                  "author_flair_css_class" : null,
                  "author_flair_text" : null,
                  "banned_by" : null,
                  "body" : "Can someone explain me why it's so shocking a JVM writing in Go?",
                  "body_html" : "<div class=\"md\"><p>Can someone explain me why it&#39;s so shocking a JVM writing in Go?</p>\n</div>",
                  "controversiality" : 0,
                  "created" : 1426405175.0,
                  "created_utc" : 1426376375.0,
                  "distinguished" : null,
                  "downs" : 0,
                  "edited" : false,
                  "gilded" : 0,
                  "id" : "cpf3eo2",
                  "likes" : null,
                  "link_id" : "t3_2z1frh",
                  "mod_reports" : [  ],
                  "name" : "t1_cpf3eo2",
                  "num_reports" : null,
                  "parent_id" : "t3_2z1frh",
                  "replies" : { "data" : { "after" : null,
                          "before" : null,
                          "children" : [ { "data" : { "approved_by" : null,
                                    "archived" : false,
                                    "author" : "RIC_FLAIR-WOOO",
                                    "author_flair_css_class" : null,
                                    "author_flair_text" : null,
                                    "banned_by" : null,
                                    "body" : "It's not. These comments are just really silly.\n\nMuch better discussion [here](https://news.ycombinator.com/item?id=9203045)",
                                    "body_html" : "<div class=\"md\"><p>It&#39;s not. These comments are just really silly.</p>\n\n<p>Much better discussion <a href=\"https://news.ycombinator.com/item?id=9203045\">here</a></p>\n</div>",
                                    "controversiality" : 0,
                                    "created" : 1426409612.0,
                                    "created_utc" : 1426380812.0,
                                    "distinguished" : null,
                                    "downs" : 0,
                                    "edited" : false,
                                    "gilded" : 0,
                                    "id" : "cpf5hgv",
                                    "likes" : null,
                                    "link_id" : "t3_2z1frh",
                                    "mod_reports" : [  ],
                                    "name" : "t1_cpf5hgv",
                                    "num_reports" : null,
                                    "parent_id" : "t1_cpf3eo2",
                                    "replies" : { "data" : { "after" : null,
                                            "before" : null,
                                            "children" : [ { "data" : { "approved_by" : null,
                                                      "archived" : false,
                                                      "author" : "rrubmo",
                                                      "author_flair_css_class" : null,
                                                      "author_flair_text" : null,
                                                      "banned_by" : null,
                                                      "body" : "Thanks a lot, it was quite confusing.",
                                                      "body_html" : "<div class=\"md\"><p>Thanks a lot, it was quite confusing.</p>\n</div>",
                                                      "controversiality" : 0,
                                                      "created" : 1426409937.0,
                                                      "created_utc" : 1426381137.0,
                                                      "distinguished" : null,
                                                      "downs" : 0,
                                                      "edited" : false,
                                                      "gilded" : 0,
                                                      "id" : "cpf5mwx",
                                                      "likes" : null,
                                                      "link_id" : "t3_2z1frh",
                                                      "mod_reports" : [  ],
                                                      "name" : "t1_cpf5mwx",
                                                      "num_reports" : null,
                                                      "parent_id" : "t1_cpf5hgv",
                                                      "replies" : "",
                                                      "report_reasons" : null,
                                                      "saved" : false,
                                                      "score" : 4,
                                                      "score_hidden" : false,
                                                      "subreddit" : "golang",
                                                      "subreddit_id" : "t5_2rc7j",
                                                      "ups" : 4,
                                                      "user_reports" : [  ]
                                                    },
                                                  "kind" : "t1"
                                                },
                                                { "data" : { "approved_by" : null,
                                                      "archived" : false,
                                                      "author" : "maester_chief",
                                                      "author_flair_css_class" : null,
                                                      "author_flair_text" : null,
                                                      "banned_by" : null,
                                                      "body" : "In general the discussion is much better at HN. Especially on Go related posts, you're likely to see comments from Russ Cox(rsc) or Andrew Gerrand(enneff) while on reddit you will see /u/dvirsky and similar users contribute quality comments like [this](http://www.reddit.com/r/golang/comments/2z1frh/a_jvm_writing_in_go/cpf2rdc)",
                                                      "body_html" : "<div class=\"md\"><p>In general the discussion is much better at HN. Especially on Go related posts, you&#39;re likely to see comments from Russ Cox(rsc) or Andrew Gerrand(enneff) while on reddit you will see <a href=\"/u/dvirsky\">/u/dvirsky</a> and similar users contribute quality comments like <a href=\"http://www.reddit.com/r/golang/comments/2z1frh/a_jvm_writing_in_go/cpf2rdc\">this</a></p>\n</div>",
                                                      "controversiality" : 0,
                                                      "created" : 1426445730.0,
                                                      "created_utc" : 1426416930.0,
                                                      "distinguished" : null,
                                                      "downs" : 0,
                                                      "edited" : false,
                                                      "gilded" : 0,
                                                      "id" : "cpfhejy",
                                                      "likes" : null,
                                                      "link_id" : "t3_2z1frh",
                                                      "mod_reports" : [  ],
                                                      "name" : "t1_cpfhejy",
                                                      "num_reports" : null,
                                                      "parent_id" : "t1_cpf5hgv",
                                                      "replies" : "",
                                                      "report_reasons" : null,
                                                      "saved" : false,
                                                      "score" : 1,
                                                      "score_hidden" : false,
                                                      "subreddit" : "golang",
                                                      "subreddit_id" : "t5_2rc7j",
                                                      "ups" : 1,
                                                      "user_reports" : [  ]
                                                    },
                                                  "kind" : "t1"
                                                }
                                              ],
                                            "modhash" : ""
                                          },
                                        "kind" : "Listing"
                                      },
                                    "report_reasons" : null,
                                    "saved" : false,
                                    "score" : 10,
                                    "score_hidden" : false,
                                    "subreddit" : "golang",
                                    "subreddit_id" : "t5_2rc7j",
                                    "ups" : 10,
                                    "user_reports" : [  ]
                                  },
                                "kind" : "t1"
                              },
                              { "data" : { "approved_by" : null,
                                    "archived" : false,
                                    "author" : "UnreachablePaul",
                                    "author_flair_css_class" : null,
                                    "author_flair_text" : null,
                                    "banned_by" : null,
                                    "body" : "Because you can't use generics",
                                    "body_html" : "<div class=\"md\"><p>Because you can&#39;t use generics</p>\n</div>",
                                    "controversiality" : 0,
                                    "created" : 1426458672.0,
                                    "created_utc" : 1426429872.0,
                                    "distinguished" : null,
                                    "downs" : 0,
                                    "edited" : false,
                                    "gilded" : 0,
                                    "id" : "cpfkrg5",
                                    "likes" : null,
                                    "link_id" : "t3_2z1frh",
                                    "mod_reports" : [  ],
                                    "name" : "t1_cpfkrg5",
                                    "num_reports" : null,
                                    "parent_id" : "t1_cpf3eo2",
                                    "replies" : "",
                                    "report_reasons" : null,
                                    "saved" : false,
                                    "score" : 1,
                                    "score_hidden" : false,
                                    "subreddit" : "golang",
                                    "subreddit_id" : "t5_2rc7j",
                                    "ups" : 1,
                                    "user_reports" : [  ]
                                  },
                                "kind" : "t1"
                              },
                              { "data" : { "approved_by" : null,
                                    "archived" : false,
                                    "author" : "jeandem",
                                    "author_flair_css_class" : null,
                                    "author_flair_text" : null,
                                    "banned_by" : null,
                                    "body" : "Because writing a virtual machine in a GCd language - impossibru. /s",
                                    "body_html" : "<div class=\"md\"><p>Because writing a virtual machine in a GCd language - impossibru. /s</p>\n</div>",
                                    "controversiality" : 0,
                                    "created" : 1426412615.0,
                                    "created_utc" : 1426383815.0,
                                    "distinguished" : null,
                                    "downs" : 0,
                                    "edited" : false,
                                    "gilded" : 0,
                                    "id" : "cpf6vh6",
                                    "likes" : null,
                                    "link_id" : "t3_2z1frh",
                                    "mod_reports" : [  ],
                                    "name" : "t1_cpf6vh6",
                                    "num_reports" : null,
                                    "parent_id" : "t1_cpf3eo2",
                                    "replies" : "",
                                    "report_reasons" : null,
                                    "saved" : false,
                                    "score" : -1,
                                    "score_hidden" : false,
                                    "subreddit" : "golang",
                                    "subreddit_id" : "t5_2rc7j",
                                    "ups" : -1,
                                    "user_reports" : [  ]
                                  },
                                "kind" : "t1"
                              },
                              { "data" : { "children" : [ "cpfd5n5" ],
                                    "count" : 1,
                                    "id" : "cpfd5n5",
                                    "name" : "t1_cpfd5n5",
                                    "parent_id" : "t1_cpf3eo2"
                                  },
                                "kind" : "more"
                              }
                            ],
                          "modhash" : ""
                        },
                      "kind" : "Listing"
                    },
                  "report_reasons" : null,
                  "saved" : false,
                  "score" : 7,
                  "score_hidden" : false,
                  "subreddit" : "golang",
                  "subreddit_id" : "t5_2rc7j",
                  "ups" : 7,
                  "user_reports" : [  ]
                },
              "kind" : "t1"
            },
            { "data" : { "approved_by" : null,
                  "archived" : false,
                  "author" : "coder543",
                  "author_flair_css_class" : null,
                  "author_flair_text" : null,
                  "banned_by" : null,
                  "body" : "doesn't it bother anyone else that both here in the title and at the top of the readme it says \"writing\" instead of \"written\"?\n\nlooks like an interesting project though.",
                  "body_html" : "<div class=\"md\"><p>doesn&#39;t it bother anyone else that both here in the title and at the top of the readme it says &quot;writing&quot; instead of &quot;written&quot;?</p>\n\n<p>looks like an interesting project though.</p>\n</div>",
                  "controversiality" : 0,
                  "created" : 1426427224.0,
                  "created_utc" : 1426398424.0,
                  "distinguished" : null,
                  "downs" : 0,
                  "edited" : false,
                  "gilded" : 0,
                  "id" : "cpfd3zq",
                  "likes" : null,
                  "link_id" : "t3_2z1frh",
                  "mod_reports" : [  ],
                  "name" : "t1_cpfd3zq",
                  "num_reports" : null,
                  "parent_id" : "t3_2z1frh",
                  "replies" : { "data" : { "after" : null,
                          "before" : null,
                          "children" : [ { "data" : { "approved_by" : null,
                                    "archived" : false,
                                    "author" : "UnreachablePaul",
                                    "author_flair_css_class" : null,
                                    "author_flair_text" : null,
                                    "banned_by" : null,
                                    "body" : "Because author likes Java and that probably amounts to being mentally challenged in a funny way",
                                    "body_html" : "<div class=\"md\"><p>Because author likes Java and that probably amounts to being mentally challenged in a funny way</p>\n</div>",
                                    "controversiality" : 0,
                                    "created" : 1426441149.0,
                                    "created_utc" : 1426412349.0,
                                    "distinguished" : null,
                                    "downs" : 0,
                                    "edited" : false,
                                    "gilded" : 0,
                                    "id" : "cpfgkdw",
                                    "likes" : null,
                                    "link_id" : "t3_2z1frh",
                                    "mod_reports" : [  ],
                                    "name" : "t1_cpfgkdw",
                                    "num_reports" : null,
                                    "parent_id" : "t1_cpfd3zq",
                                    "replies" : "",
                                    "report_reasons" : null,
                                    "saved" : false,
                                    "score" : -9,
                                    "score_hidden" : false,
                                    "subreddit" : "golang",
                                    "subreddit_id" : "t5_2rc7j",
                                    "ups" : -9,
                                    "user_reports" : [  ]
                                  },
                                "kind" : "t1"
                              } ],
                          "modhash" : ""
                        },
                      "kind" : "Listing"
                    },
                  "report_reasons" : null,
                  "saved" : false,
                  "score" : 4,
                  "score_hidden" : false,
                  "subreddit" : "golang",
                  "subreddit_id" : "t5_2rc7j",
                  "ups" : 4,
                  "user_reports" : [  ]
                },
              "kind" : "t1"
            },
            { "data" : { "approved_by" : null,
                  "archived" : false,
                  "author" : "meowtasticly",
                  "author_flair_css_class" : null,
                  "author_flair_text" : null,
                  "banned_by" : null,
                  "body" : "Can someone explain what this is? Are they making a JVM port in Go?",
                  "body_html" : "<div class=\"md\"><p>Can someone explain what this is? Are they making a JVM port in Go?</p>\n</div>",
                  "controversiality" : 0,
                  "created" : 1426390353.0,
                  "created_utc" : 1426361553.0,
                  "distinguished" : null,
                  "downs" : 0,
                  "edited" : false,
                  "gilded" : 0,
                  "id" : "cpew9xo",
                  "likes" : null,
                  "link_id" : "t3_2z1frh",
                  "mod_reports" : [  ],
                  "name" : "t1_cpew9xo",
                  "num_reports" : null,
                  "parent_id" : "t3_2z1frh",
                  "replies" : { "data" : { "after" : null,
                          "before" : null,
                          "children" : [ { "data" : { "approved_by" : null,
                                    "archived" : false,
                                    "author" : "LimEJET",
                                    "author_flair_css_class" : null,
                                    "author_flair_text" : null,
                                    "banned_by" : null,
                                    "body" : "I guess, but it's really badly documented and there's basically no comments at all so I'm just guessing. It's probably really early in development.",
                                    "body_html" : "<div class=\"md\"><p>I guess, but it&#39;s really badly documented and there&#39;s basically no comments at all so I&#39;m just guessing. It&#39;s probably really early in development.</p>\n</div>",
                                    "controversiality" : 0,
                                    "created" : 1426393270.0,
                                    "created_utc" : 1426364470.0,
                                    "distinguished" : null,
                                    "downs" : 0,
                                    "edited" : false,
                                    "gilded" : 0,
                                    "id" : "cpexp4u",
                                    "likes" : null,
                                    "link_id" : "t3_2z1frh",
                                    "mod_reports" : [  ],
                                    "name" : "t1_cpexp4u",
                                    "num_reports" : null,
                                    "parent_id" : "t1_cpew9xo",
                                    "replies" : { "data" : { "after" : null,
                                            "before" : null,
                                            "children" : [ { "data" : { "approved_by" : null,
                                                      "archived" : false,
                                                      "author" : "uncle_bad_touches",
                                                      "author_flair_css_class" : null,
                                                      "author_flair_text" : null,
                                                      "banned_by" : null,
                                                      "body" : "Spent way more time on the graphic than the docs",
                                                      "body_html" : "<div class=\"md\"><p>Spent way more time on the graphic than the docs</p>\n</div>",
                                                      "controversiality" : 0,
                                                      "created" : 1426406565.0,
                                                      "created_utc" : 1426377765.0,
                                                      "distinguished" : null,
                                                      "downs" : 0,
                                                      "edited" : false,
                                                      "gilded" : 0,
                                                      "id" : "cpf42mr",
                                                      "likes" : null,
                                                      "link_id" : "t3_2z1frh",
                                                      "mod_reports" : [  ],
                                                      "name" : "t1_cpf42mr",
                                                      "num_reports" : null,
                                                      "parent_id" : "t1_cpexp4u",
                                                      "replies" : "",
                                                      "report_reasons" : null,
                                                      "saved" : false,
                                                      "score" : 4,
                                                      "score_hidden" : false,
                                                      "subreddit" : "golang",
                                                      "subreddit_id" : "t5_2rc7j",
                                                      "ups" : 4,
                                                      "user_reports" : [  ]
                                                    },
                                                  "kind" : "t1"
                                                },
                                                { "data" : { "approved_by" : null,
                                                      "archived" : false,
                                                      "author" : "devsquid",
                                                      "author_flair_css_class" : null,
                                                      "author_flair_text" : null,
                                                      "banned_by" : null,
                                                      "body" : "Or someone is just doing it for class or something and wants to share",
                                                      "body_html" : "<div class=\"md\"><p>Or someone is just doing it for class or something and wants to share</p>\n</div>",
                                                      "controversiality" : 0,
                                                      "created" : 1426408818.0,
                                                      "created_utc" : 1426380018.0,
                                                      "distinguished" : null,
                                                      "downs" : 0,
                                                      "edited" : false,
                                                      "gilded" : 0,
                                                      "id" : "cpf54jd",
                                                      "likes" : null,
                                                      "link_id" : "t3_2z1frh",
                                                      "mod_reports" : [  ],
                                                      "name" : "t1_cpf54jd",
                                                      "num_reports" : null,
                                                      "parent_id" : "t1_cpexp4u",
                                                      "replies" : "",
                                                      "report_reasons" : null,
                                                      "saved" : false,
                                                      "score" : 2,
                                                      "score_hidden" : false,
                                                      "subreddit" : "golang",
                                                      "subreddit_id" : "t5_2rc7j",
                                                      "ups" : 2,
                                                      "user_reports" : [  ]
                                                    },
                                                  "kind" : "t1"
                                                }
                                              ],
                                            "modhash" : ""
                                          },
                                        "kind" : "Listing"
                                      },
                                    "report_reasons" : null,
                                    "saved" : false,
                                    "score" : 3,
                                    "score_hidden" : false,
                                    "subreddit" : "golang",
                                    "subreddit_id" : "t5_2rc7j",
                                    "ups" : 3,
                                    "user_reports" : [  ]
                                  },
                                "kind" : "t1"
                              } ],
                          "modhash" : ""
                        },
                      "kind" : "Listing"
                    },
                  "report_reasons" : null,
                  "saved" : false,
                  "score" : 1,
                  "score_hidden" : false,
                  "subreddit" : "golang",
                  "subreddit_id" : "t5_2rc7j",
                  "ups" : 1,
                  "user_reports" : [  ]
                },
              "kind" : "t1"
            },
            { "data" : { "approved_by" : null,
                  "archived" : false,
                  "author" : "dvirsky",
                  "author_flair_css_class" : null,
                  "author_flair_text" : null,
                  "banned_by" : null,
                  "body" : "YO DAWG, I HERD YOU LIKE GC PAUSES, SO I BUILT A JVM WITH A RUNTIME THAT HAS GC, SO YOUR JVM CAN HAVE GC PAUSES WHILE IT HAS GC PAUSES.",
                  "body_html" : "<div class=\"md\"><p>YO DAWG, I HERD YOU LIKE GC PAUSES, SO I BUILT A JVM WITH A RUNTIME THAT HAS GC, SO YOUR JVM CAN HAVE GC PAUSES WHILE IT HAS GC PAUSES.</p>\n</div>",
                  "controversiality" : 0,
                  "created" : 1426403794.0,
                  "created_utc" : 1426374994.0,
                  "distinguished" : null,
                  "downs" : 0,
                  "edited" : false,
                  "gilded" : 0,
                  "id" : "cpf2rdc",
                  "likes" : null,
                  "link_id" : "t3_2z1frh",
                  "mod_reports" : [  ],
                  "name" : "t1_cpf2rdc",
                  "num_reports" : null,
                  "parent_id" : "t3_2z1frh",
                  "replies" : { "data" : { "after" : null,
                          "before" : null,
                          "children" : [ { "data" : { "approved_by" : null,
                                    "archived" : false,
                                    "author" : "RIC_FLAIR-WOOO",
                                    "author_flair_css_class" : null,
                                    "author_flair_text" : null,
                                    "banned_by" : null,
                                    "body" : "While that is a pretty dank meme you've posted, this implementation uses Go's native GC only.",
                                    "body_html" : "<div class=\"md\"><p>While that is a pretty dank meme you&#39;ve posted, this implementation uses Go&#39;s native GC only.</p>\n</div>",
                                    "controversiality" : 0,
                                    "created" : 1426410575.0,
                                    "created_utc" : 1426381775.0,
                                    "distinguished" : null,
                                    "downs" : 0,
                                    "edited" : false,
                                    "gilded" : 0,
                                    "id" : "cpf5xih",
                                    "likes" : null,
                                    "link_id" : "t3_2z1frh",
                                    "mod_reports" : [  ],
                                    "name" : "t1_cpf5xih",
                                    "num_reports" : null,
                                    "parent_id" : "t1_cpf2rdc",
                                    "replies" : { "data" : { "after" : null,
                                            "before" : null,
                                            "children" : [ { "data" : { "approved_by" : null,
                                                      "archived" : false,
                                                      "author" : "dvirsky",
                                                      "author_flair_css_class" : null,
                                                      "author_flair_text" : null,
                                                      "banned_by" : null,
                                                      "body" : "that's interesting. had there been any documentation I would have known that :/ \n(but I wouldn't have missed the joke opportunity regardless)",
                                                      "body_html" : "<div class=\"md\"><p>that&#39;s interesting. had there been any documentation I would have known that :/ \n(but I wouldn&#39;t have missed the joke opportunity regardless)</p>\n</div>",
                                                      "controversiality" : 0,
                                                      "created" : 1426436084.0,
                                                      "created_utc" : 1426407284.0,
                                                      "distinguished" : null,
                                                      "downs" : 0,
                                                      "edited" : false,
                                                      "gilded" : 0,
                                                      "id" : "cpffkil",
                                                      "likes" : null,
                                                      "link_id" : "t3_2z1frh",
                                                      "mod_reports" : [  ],
                                                      "name" : "t1_cpffkil",
                                                      "num_reports" : null,
                                                      "parent_id" : "t1_cpf5xih",
                                                      "replies" : { "data" : { "after" : null,
                                                              "before" : null,
                                                              "children" : [ { "data" : { "approved_by" : null,
                                                                        "archived" : false,
                                                                        "author" : "jeandem",
                                                                        "author_flair_css_class" : null,
                                                                        "author_flair_text" : null,
                                                                        "banned_by" : null,
                                                                        "body" : "> had there been any documentation I would have known that :/\n\nLack of documentation is not an excuse for blindly guessing. ;)",
                                                                        "body_html" : "<div class=\"md\"><blockquote>\n<p>had there been any documentation I would have known that :/</p>\n</blockquote>\n\n<p>Lack of documentation is not an excuse for blindly guessing. ;)</p>\n</div>",
                                                                        "controversiality" : 0,
                                                                        "created" : 1426485428.0,
                                                                        "created_utc" : 1426456628.0,
                                                                        "distinguished" : null,
                                                                        "downs" : 0,
                                                                        "edited" : false,
                                                                        "gilded" : 0,
                                                                        "id" : "cpfyg8y",
                                                                        "likes" : null,
                                                                        "link_id" : "t3_2z1frh",
                                                                        "mod_reports" : [  ],
                                                                        "name" : "t1_cpfyg8y",
                                                                        "num_reports" : null,
                                                                        "parent_id" : "t1_cpffkil",
                                                                        "replies" : "",
                                                                        "report_reasons" : null,
                                                                        "saved" : false,
                                                                        "score" : 1,
                                                                        "score_hidden" : false,
                                                                        "subreddit" : "golang",
                                                                        "subreddit_id" : "t5_2rc7j",
                                                                        "ups" : 1,
                                                                        "user_reports" : [  ]
                                                                      },
                                                                    "kind" : "t1"
                                                                  } ],
                                                              "modhash" : ""
                                                            },
                                                          "kind" : "Listing"
                                                        },
                                                      "report_reasons" : null,
                                                      "saved" : false,
                                                      "score" : 0,
                                                      "score_hidden" : false,
                                                      "subreddit" : "golang",
                                                      "subreddit_id" : "t5_2rc7j",
                                                      "ups" : 0,
                                                      "user_reports" : [  ]
                                                    },
                                                  "kind" : "t1"
                                                } ],
                                            "modhash" : ""
                                          },
                                        "kind" : "Listing"
                                      },
                                    "report_reasons" : null,
                                    "saved" : false,
                                    "score" : 15,
                                    "score_hidden" : false,
                                    "subreddit" : "golang",
                                    "subreddit_id" : "t5_2rc7j",
                                    "ups" : 15,
                                    "user_reports" : [  ]
                                  },
                                "kind" : "t1"
                              },
                              { "data" : { "approved_by" : null,
                                    "archived" : false,
                                    "author" : "ChasingLogic",
                                    "author_flair_css_class" : null,
                                    "author_flair_text" : null,
                                    "banned_by" : null,
                                    "body" : "This made me laugh so hard my wife was like what and then just gave me a weird look. (She's not a programmer.)",
                                    "body_html" : "<div class=\"md\"><p>This made me laugh so hard my wife was like what and then just gave me a weird look. (She&#39;s not a programmer.)</p>\n</div>",
                                    "controversiality" : 0,
                                    "created" : 1426409685.0,
                                    "created_utc" : 1426380885.0,
                                    "distinguished" : null,
                                    "downs" : 0,
                                    "edited" : false,
                                    "gilded" : 0,
                                    "id" : "cpf5in0",
                                    "likes" : null,
                                    "link_id" : "t3_2z1frh",
                                    "mod_reports" : [  ],
                                    "name" : "t1_cpf5in0",
                                    "num_reports" : null,
                                    "parent_id" : "t1_cpf2rdc",
                                    "replies" : "",
                                    "report_reasons" : null,
                                    "saved" : false,
                                    "score" : 1,
                                    "score_hidden" : false,
                                    "subreddit" : "golang",
                                    "subreddit_id" : "t5_2rc7j",
                                    "ups" : 1,
                                    "user_reports" : [  ]
                                  },
                                "kind" : "t1"
                              }
                            ],
                          "modhash" : ""
                        },
                      "kind" : "Listing"
                    },
                  "report_reasons" : null,
                  "saved" : false,
                  "score" : 10,
                  "score_hidden" : false,
                  "subreddit" : "golang",
                  "subreddit_id" : "t5_2rc7j",
                  "ups" : 10,
                  "user_reports" : [  ]
                },
              "kind" : "t1"
            },
            { "data" : { "approved_by" : null,
                  "archived" : false,
                  "author" : "dominosci",
                  "author_flair_css_class" : null,
                  "author_flair_text" : null,
                  "banned_by" : null,
                  "body" : "WAT?!",
                  "body_html" : "<div class=\"md\"><p>WAT?!</p>\n</div>",
                  "controversiality" : 0,
                  "created" : 1426388510.0,
                  "created_utc" : 1426359710.0,
                  "distinguished" : null,
                  "downs" : 0,
                  "edited" : false,
                  "gilded" : 0,
                  "id" : "cpevdan",
                  "likes" : null,
                  "link_id" : "t3_2z1frh",
                  "mod_reports" : [  ],
                  "name" : "t1_cpevdan",
                  "num_reports" : null,
                  "parent_id" : "t3_2z1frh",
                  "replies" : { "data" : { "after" : null,
                          "before" : null,
                          "children" : [ { "data" : { "approved_by" : null,
                                    "archived" : false,
                                    "author" : "koffiezet",
                                    "author_flair_css_class" : null,
                                    "author_flair_text" : null,
                                    "banned_by" : null,
                                    "body" : "I came here to say this...\n\nBut - if it really works, it'd be really cool though :P Not gonna try it out though...\n",
                                    "body_html" : "<div class=\"md\"><p>I came here to say this...</p>\n\n<p>But - if it really works, it&#39;d be really cool though :P Not gonna try it out though...</p>\n</div>",
                                    "controversiality" : 0,
                                    "created" : 1426395497.0,
                                    "created_utc" : 1426366697.0,
                                    "distinguished" : null,
                                    "downs" : 0,
                                    "edited" : false,
                                    "gilded" : 0,
                                    "id" : "cpeysw8",
                                    "likes" : null,
                                    "link_id" : "t3_2z1frh",
                                    "mod_reports" : [  ],
                                    "name" : "t1_cpeysw8",
                                    "num_reports" : null,
                                    "parent_id" : "t1_cpevdan",
                                    "replies" : "",
                                    "report_reasons" : null,
                                    "saved" : false,
                                    "score" : 1,
                                    "score_hidden" : false,
                                    "subreddit" : "golang",
                                    "subreddit_id" : "t5_2rc7j",
                                    "ups" : 1,
                                    "user_reports" : [  ]
                                  },
                                "kind" : "t1"
                              } ],
                          "modhash" : ""
                        },
                      "kind" : "Listing"
                    },
                  "report_reasons" : null,
                  "saved" : false,
                  "score" : -5,
                  "score_hidden" : false,
                  "subreddit" : "golang",
                  "subreddit_id" : "t5_2rc7j",
                  "ups" : -5,
                  "user_reports" : [  ]
                },
              "kind" : "t1"
            },
            { "data" : { "approved_by" : null,
                  "archived" : false,
                  "author" : "ksawicki",
                  "author_flair_css_class" : null,
                  "author_flair_text" : null,
                  "banned_by" : null,
                  "body" : "lol",
                  "body_html" : "<div class=\"md\"><p>lol</p>\n</div>",
                  "controversiality" : 0,
                  "created" : 1426393573.0,
                  "created_utc" : 1426364773.0,
                  "distinguished" : null,
                  "downs" : 0,
                  "edited" : false,
                  "gilded" : 0,
                  "id" : "cpexufw",
                  "likes" : null,
                  "link_id" : "t3_2z1frh",
                  "mod_reports" : [  ],
                  "name" : "t1_cpexufw",
                  "num_reports" : null,
                  "parent_id" : "t3_2z1frh",
                  "replies" : "",
                  "report_reasons" : null,
                  "saved" : false,
                  "score" : -6,
                  "score_hidden" : false,
                  "subreddit" : "golang",
                  "subreddit_id" : "t5_2rc7j",
                  "ups" : -6,
                  "user_reports" : [  ]
                },
              "kind" : "t1"
            }
          ],
        "modhash" : ""
      },
    "kind" : "Listing"
  }
]
@ChimeraCoder
Copy link
Owner

Aha - I know where this error is: https://github.com/ChimeraCoder/gojson/blob/master/json-to-struct.go#L116

Basically, gojson tries to unmarshal it and then reflect on what the possible type is. At the top-level, JSON can either be an object or an array (slice) of objects. (It can also be a literal or null, but you're probably not using gojson if the entire data is just a literal or null!). In Go, an object is unmarshalled by default to a map[string]interface{}, hence the first case. However, the array is being unmarshalled to []interface{}, which isn't one of the options here, so we're seeing the error from the default case.

So that's a bug which we definitely need to fix.

In your case, though, there's still a second issue: the array appears to be heterogeneous (correct me if I'm wrong). That is, the first element has different fields from the second, third, etc., and there is a field name which distinguishes the kind - this is essentially a tagged union.

In this case, gojson could do a few things:

  1. Provide a super-struct which is the union of all the fields it finds, and then you can use []SuperStruct as a target to unmarshal this endpoint
  2. Provide an actual []map[string]interface{} for you to use as the recipient (this isn't really doing any work)
  3. Provide an []interface{} for you to use as the recipient (again, not really doing any work).

The first option is definitely doable, and I'll add a ticket for that.

As a stop-gap, I'd recommend simply doing $ curl -o https://www.reddit.com/r/golang/comments/2z1frh/a_jvm_writing_in_go.json; $EDITOR a_jv_writing_in_go.json; cat a_jvm_writing_in_go.json | gojson - where the middle step is simply opening a text editor and deleting everything except the first object in the array. That way you don't have to write out the struct definition manually, though once we implement this feature, that would mean you could omit this extra step.

@jbuberel
Copy link
Author

Thanks for the quick reply.

Regarding option 1 - I can see providing both the []SuperStruct (the union) or the []SubStruct (the intersection).

Regarding option 2 - While not doing much work, it is a big convenience. Seems like it would be simpler to implement.

My vote would be to implement and release them in this order:

  • Provide []map[string]interface{} support
  • Provide []SuperStruct support, with a flag to select which to use
  • Provide []SubStruct support, with additional flag selector
> gojson -hetero=[map|union|intersect]

ChimeraCoder added a commit that referenced this issue Mar 29, 2015
* Previously this would panic

* Partial fix for #17
@ChimeraCoder
Copy link
Owner

Alright, so I've updated gojson so that, as of c587013, top-level arrays will simply generate a type aliased to []interface{}. This isn't as ideal as having it recurse down into the elements and compare them, but it's a quicker fix, and now it won't panic on this input

The SuperStruct/SubStruct is closely linked to some other things in the pipeline, so I think we'll be able to address that all together.

@qiukeren
Copy link

+1 need for mapping array(struct)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants