Newbies troubles w/ JSON

Hi,

I am trying to decode byte array but am unable to unmarshal data into struct. I get an error when trying to access index 1 of array. Can anyone see the error?

package main

import (
	"encoding/json"
	"fmt"
)

// Symbols struct
type symbols struct {
	SymbolID string `json:"symbol_id"`
}

func main() {

	

	var text = []byte(`[
		{
		  "symbol_id": "BITSTAMP_SPOT_BTC_USD",
		  "exchange_id": "BITSTAMP",
		  "symbol_type": "SPOT",
		  "asset_id_base": "BTC",
		  "asset_id_quote": "USD"
		},
		{
		  "symbol_id": "BITMEX_FTS_BTC_USD_170630",
		  "exchange_id" : "BITMEX",
		  "symbol_type": "FUTURES",
		  "future_delivery_time": "2017-06-30T10:00:00.000000Z"
		  "asset_id_base": "BTC",
		  "asset_id_quote": "USD",
		},
		{
		  "symbol_id": "DERIBIT_OPT_BTC_USD_170331_500_P",
		  "exchange_id": "DERIBIT",
		  "symbol_type": "OPTION",
		  "option_type_is_call": false,
		  "option_strike_price": 500.0,
		  "option_contract_unit": 1,
		  "option_exercise_style": "EUROPEAN",
		  "option_expiration_time": "2017-03-31T10:00:00.000000Z",
		  "asset_id_base": "BTC",
		  "asset_id_quote": "USD"
		}
	  ]`)

	var sym []symbols
	json.Unmarshal(text, &sym)

	fmt.Println(sym[1].SymbolID)

}

You should check for the return of json.Unmarshal, it tells you this:

invalid character '"' after object key:value pair

Since thats not very helpful, I threw your JSON at jsonlint.com:

Error: Parse error on line 12:
...10:00:00.000000Z"		"asset_id_base": "BT
----------------------^
Expecting 'EOF', '}', ':', ',', ']', got 'STRING'

After adding a comma, there was this:

Error: Parse error on line 14:
..._id_quote": "USD",	},	{		"symbol_id":
----------------------^
Expecting 'STRING', got '}'

So, that comma removed, the JSON was reported as valid.

You can find my modified version at the playground:

https://play.golang.org/p/InYByDGGMOW

1 Like

I learned another way to debug, thank you.

What do you mean?

Checking a return value of error should be a quite well known thing and is a good habbit, while panicing on it is not but good enough for such a small piece of code. To be honest, I took this for granted, and wanted to start with only mentioning that without even trying your code.

The other technique used, was just inspired by google, entering “json validator” into the search box and took the first one. There was not much more into this…

At the end, if you check error returns and know how to use google, most things are easily solvable. What makes debugging hard, is to ignore half of the available data.

1 Like

I never used a JSON validator before, so thank you. I will take your advise on starting with return value of error as the start of debugging process. Interestingly enough, the REST API I am consuming data from gave the JSON snippet in their documentation with those errors. However, when I tried to unmarshal the data straight from a request from their API, it worked.

Yeah, broken examples are annoying.

If you’re handing some data in some format, it’s best to make sure the data is valid by using external validators. For example, you could’ve piped that JSON through python -mjson.tool to see if it outputs the same JSON back. If it does, then proceed with the debugging of your code!

In your application you can use https://github.com/clbanning/checkjson to validate input. Or you can use the ResolveJSONError function to just get better error info after calling json.Unmarshal.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.