go icon indicating copy to clipboard operation
go copied to clipboard

Fails to parse valid JSON

Open eatonphil opened this issue 3 years ago • 1 comments

I have a valid JSON object.

{
  "id": "2489657257",
  "type": "PushEvent",
  "actor": {
    "id": 3170981,
    "login": "kulmala",
    "gravatar_id": "",
    "url": "https://api.github.com/users/kulmala",
    "avatar_url": "https://avatars.githubusercontent.com/u/3170981?"
  },
  "repo": {
    "id": 23549741,
    "name": "kulmala/metrics",
    "url": "https://api.github.com/repos/kulmala/metrics"
  },
  "payload": {
    "push_id": 536866759,
    "size": 1,
    "distinct_size": 1,
    "ref": "refs/heads/gh-pages",
    "head": "c1345d8d6ee6b1fcbb4bf98df5381b83a2d1d6c7",
    "before": "c4a492172b39d83a510142c164cef5b9aab15f2e",
    "commits": [
      {
        "sha": "c1345d8d6ee6b1fcbb4bf98df5381b83a2d1d6c7",
        "author": {
          "email": "[email protected]",
          "name": "Jaana Kulmala"
        },
        "message": "Site updated to bdfecdb",
        "distinct": true,
        "url": "https://api.github.com/repos/kulmala/metrics/commits/c1345d8d6ee6b1fcbb4bf98df5381b83a2d1d6c7"
      }
    ]
  },
  "public": true,
  "created_at": "2015-01-01T15:13:25Z"
}

But when I pass it through json-iterator/go I get:

2022/07/11 18:50:13 ReadMapCB: expect { or n, but found , error found in #10 byte of ...|5:13:25Z"}
|..., bigger context ...|"public":true,"created_at":"2015-01-01T15:13:25Z"}
|...

eatonphil avatar Jul 11 '22 18:07 eatonphil

Hey, check the decoding type. I wrote a small test and it works great.

package main

import (
	"fmt"
	"time"

	jsonIter "github.com/json-iterator/go"
)

var jsonIt = jsonIter.ConfigCompatibleWithStandardLibrary

type ExpectedJson struct {
	ID          string     `json:"id"`
	Public      bool       `json:"public"`
	DateCreated *time.Time `json:"created_at"`
}

func main() {
	str := `{
  "id": "2489657257",
  "type": "PushEvent",
  "actor": {
    "id": 3170981,
    "login": "kulmala",
    "gravatar_id": "",
    "url": "https://api.github.com/users/kulmala",
    "avatar_url": "https://avatars.githubusercontent.com/u/3170981?"
  },
  "repo": {
    "id": 23549741,
    "name": "kulmala/metrics",
    "url": "https://api.github.com/repos/kulmala/metrics"
  },
  "payload": {
    "push_id": 536866759,
    "size": 1,
    "distinct_size": 1,
    "ref": "refs/heads/gh-pages",
    "head": "c1345d8d6ee6b1fcbb4bf98df5381b83a2d1d6c7",
    "before": "c4a492172b39d83a510142c164cef5b9aab15f2e",
    "commits": [
      {
        "sha": "c1345d8d6ee6b1fcbb4bf98df5381b83a2d1d6c7",
        "author": {
          "email": "[email protected]",
          "name": "Jaana Kulmala"
        },
        "message": "Site updated to bdfecdb",
        "distinct": true,
        "url": "https://api.github.com/repos/kulmala/metrics/commits/c1345d8d6ee6b1fcbb4bf98df5381b83a2d1d6c7"
      }
    ]
  },
  "public": true,
  "created_at": "2015-01-01T15:13:25Z"
}`

	var event ExpectedJson

	err := jsonIt.Unmarshal([]byte(str), &event)
	if err != nil {
		panic(err)
	}

	fmt.Print(event)

}

sapsan4eg avatar Jul 16 '22 05:07 sapsan4eg