Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing JSON 'NaN' value in Go

Tags:

json

nan

go

When I try and unmarshal this JSON object from the Microsoft Web Ngram API:

{"backoff": NaN, "cookie": "", "probabilities": [], "words": []}

I get the error: "invalid character 'N' looking for beginning of value"

I know that NaN isn't valid JSON but the data isn't mine and I need a way to parse it. Is there any easy way to do this in Go?

like image 989
a3onstorm Avatar asked Jul 28 '14 20:07

a3onstorm


1 Answers

You could replace it with null (or 0 or whatever is acceptable):

b, err := ioutil.ReadAll(resp)
//check err
b = bytes.Replace(b, []byte(":NaN"), []byte(":null"), -1) 

//json.Decode(b)
like image 184
OneOfOne Avatar answered Sep 27 '22 19:09

OneOfOne