Code Monkey home page Code Monkey logo

jsonutils's Introduction

jsonutils

Converter for JSON data to a Go struct or a Java class for GSON

go get github.com/bashtian/jsonutils/cmd/jsonutil

You can print the structure of a JSON from a URL

jsonutil https://api.github.com/repos/bashtian/jsonutils

or from a file

jsonutil -f file.json

or from stdin

echo '{"some": "data"}' | jsonutil

If you want to print example data as comments, use the -x parameter

jsonutil -x https://api.github.com/repos/bashtian/jsonutils

You can also print Java code

jsonutil -j https://api.github.com/repos/bashtian/jsonutils

Example

JSON

{
    "firstName": "John",
    "lastName": "Smith",
    "age": 25,
    "balance": 123.45,
    "address": {
        "streetAddress": "21 2nd Street",
        "city": "New York",
        "state": "NY",
        "postalCode": "10021"
    },
    "phoneNumber": [
        {
            "type": "home",
            "number": "212 555-1234",
            "last_call": "2013-01-10T05:27:07Z"
        },
        {
            "type": "fax",
            "number": "646 555-4567",
            "last_call": "2013-01-10T05:27:07Z"
        }
    ],
    "tags": ["music","video"]
}

Go

jsonutil -x -c=false -f Example.json
type Example struct {
	Address struct {
		City          string `json:"city"`          // New York
		PostalCode    string `json:"postalCode"`    // 10021
		State         string `json:"state"`         // NY
		StreetAddress string `json:"streetAddress"` // 21 2nd Street
	} `json:"address"`
	Age         int64   `json:"age"`       // 25
	Balance     float64 `json:"balance"`   // 123.45
	FirstName   string  `json:"firstName"` // John
	LastName    string  `json:"lastName"`  // Smith
	PhoneNumber []struct {
		LastCall string `json:"last_call"` // 2013-01-10T05:27:07Z
		Number   string `json:"number"`    // 212 555-1234
		Type     string `json:"type"`      // home
	} `json:"phoneNumber"`
	Tags []string `json:"tags"` // music
}
jsonutils -f Example.json
type Example struct {
	Address struct {
		City          string `json:"city"`
		PostalCode    int64  `json:"postalCode,string"`
		State         string `json:"state"`
		StreetAddress string `json:"streetAddress"`
	} `json:"address"`
	Age         int64   `json:"age"`
	Balance     float64 `json:"balance"`
	FirstName   string  `json:"firstName"`
	LastName    string  `json:"lastName"`
	PhoneNumber []struct {
		LastCall time.Time `json:"last_call"`
		Number   string    `json:"number"`
		Type     string    `json:"type"`
	} `json:"phoneNumber"`
	Tags []string `json:"tags"`
}

jsonutils's People

Contributors

bashtian avatar bryant1410 avatar josharian avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

jsonutils's Issues

`panic: json: cannot unmarshal string into Go value of type []string`

I ran jsonutils on what the New York Times top stories API, and it spit out what looked like the correct format. But I'm getting panic: json: cannot unmarshal string into Go value of type []string when I run my code. Is this user error?

For reference, here's a sample API response:

{
  "status": "OK",
  "copyright": "Copyright (c) 2015 The New York Times Company. All Rights Reserved.",
  "section": "politics",
  "last_updated": "2015-12-27T14:40:04-05:00",
  "num_results": 26,
  "results": [
    {
      "section": "N.Y. / Region",
      "subsection": "",
      "title": "Christie Spins His Version of Security Record on Trail",
      "abstract": "An examination of Gov. Chris Christie’s record as New Jersey’s top federal prosecutor shows that he has, at times, overstated the significance of the terrorism prosecutions he oversaw.",
      "url": "http://www.nytimes.com/2015/12/27/nyregion/Christie-markets-himself-as-protector-to-gain-in-polls.html",
      "byline": "By ALEXANDER BURNS and CHARLIE SAVAGE",
      "item_type": "Article",
      "updated_date": "2015-12-26T18:04:19-5:00",
      "created_date": "2015-12-26T12:13:30-5:00",
      "published_date": "2015-12-27T00:00:00-5:00",
      "material_type_facet": "News",
      "kicker": "",
      "des_facet": [
        "Presidential Election of 2016",
        "Terrorism"
      ],
      "org_facet": [
        "Republican Party"
      ],
      "per_facet": [
        "Christie, Christopher J"
      ],
      "geo_facet": [
        "New Jersey"
      ],
      "multimedia": [
        {
          "url": "http://static01.nyt.com/images/2015/12/27/nyregion/27CHRISTIE1/27CHRISTIE1-thumbStandard.jpg",
          "format": "Standard Thumbnail",
          "height": 75,
          "width": 75,
          "type": "image",
          "subtype": "photo",
          "caption": "Gov. Chris Christie of New Jersey spoke about the Sept. 11, 2001, attacks at a Republican conference last month.",
          "copyright": "Stephen Crowley/The New York Times"
        },
        {
          "url": "http://static01.nyt.com/images/2015/12/27/nyregion/27CHRISTIE1/27CHRISTIE1-thumbLarge.jpg",
          "format": "thumbLarge",
          "height": 150,
          "width": 150,
          "type": "image",
          "subtype": "photo",
          "caption": "Gov. Chris Christie of New Jersey spoke about the Sept. 11, 2001, attacks at a Republican conference last month.",
          "copyright": "Stephen Crowley/The New York Times"
        },
        {
          "url": "http://static01.nyt.com/images/2015/12/27/nyregion/27CHRISTIE1/27CHRISTIE1-articleInline.jpg",
          "format": "Normal",
          "height": 126,
          "width": 190,
          "type": "image",
          "subtype": "photo",
          "caption": "Gov. Chris Christie of New Jersey spoke about the Sept. 11, 2001, attacks at a Republican conference last month.",
          "copyright": "Stephen Crowley/The New York Times"
        },
        {
          "url": "http://static01.nyt.com/images/2015/12/27/nyregion/27CHRISTIE1/27CHRISTIE1-mediumThreeByTwo210.jpg",
          "format": "mediumThreeByTwo210",
          "height": 140,
          "width": 210,
          "type": "image",
          "subtype": "photo",
          "caption": "Gov. Chris Christie of New Jersey spoke about the Sept. 11, 2001, attacks at a Republican conference last month.",
          "copyright": "Stephen Crowley/The New York Times"
        },
        {
          "url": "http://static01.nyt.com/images/2015/12/27/nyregion/27CHRISTIE1/27CHRISTIE1-superJumbo.jpg",
          "format": "superJumbo",
          "height": 1363,
          "width": 2048,
          "type": "image",
          "subtype": "photo",
          "caption": "Gov. Chris Christie of New Jersey spoke about the Sept. 11, 2001, attacks at a Republican conference last month.",
          "copyright": "Stephen Crowley/The New York Times"
        }
      ]
    }
]

Accordingly, jsonutils gave this structure:

type PoliticsJson struct {
    Copyright   string    `json:"copyright"`
    LastUpdated time.Time `json:"last_updated"`
    NumResults  int64     `json:"num_results"`
    Results     []struct {
        Abstract          string   `json:"abstract"`
        Byline            string   `json:"byline"`
        CreatedDate       string   `json:"created_date"`
        DesFacet          []string `json:"des_facet"`
        GeoFacet          []string `json:"geo_facet"`
        ItemType          string   `json:"item_type"`
        Kicker            string   `json:"kicker"`
        MaterialTypeFacet string   `json:"material_type_facet"`
        Multimedia        []struct {
            Caption   string `json:"caption"`
            Copyright string `json:"copyright"`
            Format    string `json:"format"`
            Height    int64  `json:"height"`
            Subtype   string `json:"subtype"`
            Type      string `json:"type"`
            URL       string `json:"url"`
            Width     int64  `json:"width"`
        } `json:"multimedia"`
        OrgFacet      []string `json:"org_facet"`
        PerFacet      []string `json:"per_facet"`
        PublishedDate string   `json:"published_date"`
        Section       string   `json:"section"`
        Subsection    string   `json:"subsection"`
        Title         string   `json:"title"`
        UpdatedDate   string   `json:"updated_date"`
        URL           string   `json:"url"`
    } `json:"results"`
    Section string `json:"section"`
    Status  string `json:"status"`
}

That looks right.

My go code is pretty simple:

package main

import (
    "encoding/json"
    "fmt"
    "io/ioutil"
    "net/http"
)

type PoliticsJson struct {
    Copyright   string    `json:"copyright"`
    LastUpdated time.Time `json:"last_updated"`
    NumResults  int64     `json:"num_results"`
    Results     []struct {
        Abstract          string   `json:"abstract"`
        Byline            string   `json:"byline"`
        CreatedDate       string   `json:"created_date"`
        DesFacet          []string `json:"des_facet"`
        GeoFacet          []string `json:"geo_facet"`
        ItemType          string   `json:"item_type"`
        Kicker            string   `json:"kicker"`
        MaterialTypeFacet string   `json:"material_type_facet"`
        Multimedia        []struct {
            Caption   string `json:"caption"`
            Copyright string `json:"copyright"`
            Format    string `json:"format"`
            Height    int64  `json:"height"`
            Subtype   string `json:"subtype"`
            Type      string `json:"type"`
            URL       string `json:"url"`
            Width     int64  `json:"width"`
        } `json:"multimedia"`
        OrgFacet      []string `json:"org_facet"`
        PerFacet      []string `json:"per_facet"`
        PublishedDate string   `json:"published_date"`
        Section       string   `json:"section"`
        Subsection    string   `json:"subsection"`
        Title         string   `json:"title"`
        UpdatedDate   string   `json:"updated_date"`
        URL           string   `json:"url"`
    } `json:"results"`
    Section string `json:"section"`
    Status  string `json:"status"`
}

func retrieveData() []byte {
    url := "http://api.nytimes.com/svc/topstories/v1/politics.json?api-key=MYAPIKEY"
    resp, err := http.Get(url)
    if err != nil {
        fmt.Println(err)
    }
    defer resp.Body.Close()
    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        fmt.Println(err)
    }
    return body
}

func main() {
    var p PoliticsJson

    err := json.Unmarshal(retrieveData(), &p)
    if err != nil {
        panic(err)
    }
    fmt.Println(p)
}

Edit

It seems to be the []strings this is having trouble with. Removing all of them but Multimedia, I get

panic: json: cannot unmarshal string into Go value of type []struct { Caption string "json:\"caption\""; Copyright string "json:\"copyright\""; Format string "json:\"format\""; Height int64 "json:\"height\""; Subtype string "json:\"subtype\""; Type string "json:\"type\""; URL string "json:\"url\""; Width int64 "json:\"width\"" }

It apparently doesn't like unmarshaling the mutlimedia key into an array of multimedia structs for some reason…?

Removing all of the fields for multimedia except URL gives this:

panic: json: cannot unmarshal string into Go value of type []struct { URL string "json:\"url\"" }

And removing the multimedia struct slice entirely gives the expected parsed JSON result. So it seems like it's treating the multimedia JSON array as a string…?

But changing it to

Multimedia string `json:"multimedia"`

Gives the error panic: json: cannot unmarshal array into Go value of type string, which suggests it _is_treating it as an array.

index out of range bug

Fails to complete for: https://github.com/brather1ng/RePoE/blob/master/data/stat_translations/monster.json

panic: runtime error: index out of range

goroutine 1 [running]:
github.com/bashtian/jsonutils.(*Model).parse(0xc000050740, 0x743de0, 0xc000062880, 0xc000068530, 0xe)
        C:/Users/home/go/src/github.com/bashtian/jsonutils/jsonutils.go:219 +0x977
github.com/bashtian/jsonutils.(*Model).parseMap(0xc000050740, 0xc00007cff0)
        C:/Users/home/go/src/github.com/bashtian/jsonutils/jsonutils.go:180 +0xd8
github.com/bashtian/jsonutils.(*Model).parse.func1()
        C:/Users/home/go/src/github.com/bashtian/jsonutils/jsonutils.go:222 +0x3a
github.com/bashtian/jsonutils.(*Model).printObject(0xc000050740, 0xc000068510, 0x7, 0x7c78ad, 0x8, 0xc000085cf8)
        C:/Users/home/go/src/github.com/bashtian/jsonutils/jsonutils.go:321 +0x164
github.com/bashtian/jsonutils.(*Model).parse(0xc000050740, 0x743de0, 0xc0000628a0, 0xc000068510, 0x7)
        C:/Users/home/go/src/github.com/bashtian/jsonutils/jsonutils.go:222 +0x4f4
github.com/bashtian/jsonutils.(*Model).parseMap(0xc000050740, 0xc00007cfc0)
        C:/Users/home/go/src/github.com/bashtian/jsonutils/jsonutils.go:180 +0xd8
github.com/bashtian/jsonutils.(*Model).writeGo.func1(0xc00007cfc0)
        C:/Users/home/go/src/github.com/bashtian/jsonutils/jsonutils.go:126 +0x3b
github.com/bashtian/jsonutils.(*Model).print(0xc000050740, 0xc000085e30, 0x7d7c23, 0x30)
        C:/Users/home/go/src/github.com/bashtian/jsonutils/jsonutils.go:173 +0xe6
github.com/bashtian/jsonutils.(*Model).writeGo(0xc000050740)
        C:/Users/home/go/src/github.com/bashtian/jsonutils/jsonutils.go:127 +0x6a
github.com/bashtian/jsonutils.(*Model).WriteGo(0xc000050740)
        C:/Users/home/go/src/github.com/bashtian/jsonutils/jsonutils.go:110 +0x99
main.main()
        C:/Users/home/go/src/github.com/bashtian/jsonutils/cmd/jsonutil/main.go:63 +0x270

Fails for uniform arrays

This may be working as intended but it did catch me by surprise; feeding in a JSON array (of either strings or ints) does not produce a useful result.

$ echo "[\"test\"]" | jsonutil
panic: interface conversion: interface is string, not map[string]interface {}
...
$ echo "[\"test\", \"more test\"]" | jsonutil
panic: interface conversion: interface is string, not map[string]interface {}
...
$ echo "[0, 1]" | jsonutil
panic: interface conversion: interface is float64, not map[string]interface {}

Git tags

Could you to use a git tag for a release?
Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.