Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I try to write a function, the code:
package main
import (
"encoding/json"
"fmt"
)
type Test struct {
Data map[string]interface{} `json:"data"`
}
func main() {
jsonStr := "{\"data\": {\"id\": 999804707614896129}}"
t := &Test{}
json.Unmarshal([]byte(jsonStr), t)
fmt.Println(int64(t.Data["id"].(float64)))
var x float64 = 999804707614896129
fmt.Println(int64(x))
}
The result is below:
999804707614896128
999804707614896128
Why the results are 999804707614896128, not 999804707614896129.
Because 999804707614896129 cannot be represented exactly with a value of type float64. float64 uses the IEEE 754 standard for representing floating point numbers. It's a format with limited precision (roughly 16 decimal digits).
If you need to "transfer" the exact number, use string and not JSON number. If the number "fits" into an int64, you will be able to parse it "exactly" (else just work with it as a string or use big.Int):
jsonStr := `{"data": {"id": "999804707614896129"}}`
t := &Test{}
if err := json.Unmarshal([]byte(jsonStr), t); err != nil {
panic(err)
}
s := t.Data["id"].(string)
fmt.Println(s)
var x int64
x, err := strconv.ParseInt(s, 10, 64)
fmt.Println(x, err)
This will output (try it on the Go Playground):
999804707614896129
999804707614896129 <nil>
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I need to parse a JSON into Go struct. Following is the struct
type Replacement struct {
Find string `json:"find"`
ReplaceWith string `json:"replaceWith"`
}
Following is an example json:
{
"find":"TestValue",
"replaceWith":""
}
The input json can have empty values for some field. Go's encoding/json library by default takes nil value for any empty string provided in JSON.
I've a downstream service, which finds and replaces the replaceWith values in configurations. This is causing issues with my downstream service as it doesn't accept nil for the replaceWith parameter. I have a workaround where I'm replacing nil values by "''" but this can cause an issue where some value is replaced with ''. Is there a way for json to not parse empty string as nil and just ""
Here is a link to the code: https://play.golang.org/p/SprPz7mnWR6
In Go string type cannot hold nil value which is zero value for pointers, interfaces, maps, slices, channels and function types, representing an uninitialized value.
When unmarshalling JSON data to struct as you do in your example ReplaceWith field will indeed be an empty string ("") - which is exactly what you are asking for.
type Replacement struct {
Find string `json:"find"`
ReplaceWith string `json:"replaceWith"`
}
func main() {
data := []byte(`
{
"find":"TestValue",
"replaceWith":""
}`)
var marshaledData Replacement
err := json.Unmarshal(data, &marshaledData)
if err != nil {
fmt.Println(err)
}
if marshaledData.ReplaceWith == "" {
fmt.Println("ReplaceWith equals to an empty string")
}
}
You can use Pointer in string and if the value is missing in JSON then it would be nil. I have done the same in past but currently I don't have code with me.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I'm currently trying to serialize some string slice in a CSV without saving output to a file. I saw some examples using bytes.Buffer but even my the smallest test doesn't work. I don't know how to debug this, the code compiles without warning and doesn't throw error after writer.Flush(). It also works correctly with saving result to a file or piping it directly to standard output
Any help would be appreciated :)
Thx.
PS: the final print is just a test. I know i can use csv.NewWriter(os.stdout) to do so (and it works) but it doesn't fit my needs. I really would like to get the result in a byte array.
package main
import (
"bytes"
"encoding/csv"
"fmt"
)
func main() {
var buffer bytes.Buffer
writer := csv.NewWriter(&buffer)
writer.Write([]string{"1", "2", "3", "4"})
writer.Write([]string{"5", "6", "7", "8"})
defer writer.Flush()
if err := writer.Error(); err != nil {
panic(err)
}
fmt.Println(buffer.Bytes())
}
You are deferring the Flush call, making it execute after the fmt.Println call. Call Flush immediately:
package main
import (
"bytes"
"encoding/csv"
"fmt"
)
func main() {
var buffer bytes.Buffer
writer := csv.NewWriter(&buffer)
writer.Write([]string{"1", "2", "3", "4"})
writer.Write([]string{"5", "6", "7", "8"})
writer.Flush()
if err := writer.Error(); err != nil {
panic(err)
}
fmt.Println(buffer.String())
}
https://play.golang.org/p/BHTyfsuf0tY
This question already has answers here:
How to iterate through a map in Golang in order?
(5 answers)
Closed 4 years ago.
How to parse this json using Go?
timelinedata := '{
"2016-08-17T00:00:00.000Z": 4,
"2016-11-02T00:00:00.000Z": 1,
"2017-08-30T00:00:00.000Z": 1
} '
I want the dates and the values in separate variables by looping over the json.
Currently I am doing it in this way
var timeline map[string]int
json.Unmarshal([]byte(timelinedata),
for k, v := range timeline {
new_key := k
new_val := v
println("val--->>", new_key, new_val)
}
The problem is that the output is not in proper order as like the json input is. Every time I run the loop the output order varies. I want to map the json in exact order as like the input. I think I am not maping it in a proper way---
You should not assume that the key order in a JSON object means anything:
From the introduction of RFC 7159 (emphasis mine):
An object is an unordered collection of zero or more name/value pairs, where a name is a string and a value is a string, number, boolean, null, object, or array.
An array is an ordered sequence of zero or more values.
In addition to that, you shouldn't assume that the producer of the JSON document has been in control of the key/value order; maps are unordered in most languages, so it mostly comes down to the used encoding library. If the producer did care about the order, they would use an array.
That being said, if you really are interested in the order of the JSON keys, you have to decode the object piece by piece, using json.Decoder.Token:
package main
import (
"encoding/json"
"fmt"
"log"
"strings"
)
func main() {
j := `{
"2016-08-17T00:00:00.000Z": 4,
"2016-11-02T00:00:00.000Z": 1,
"2017-08-30T00:00:00.000Z": 1
}`
dec := json.NewDecoder(strings.NewReader(j))
for dec.More() {
t, err := dec.Token()
if err != nil {
log.Fatal(err)
}
switch t := t.(type) {
case json.Delim:
// no-op
case string:
fmt.Printf("%s => ", t)
case float64:
fmt.Printf("%.0f\n", t)
case json.Number:
fmt.Printf(" %s\n", t)
default:
log.Fatalf("Unexpected type: %T", t)
}
}
}
Try it on the Playground: https://play.golang.org/p/qfXcOfOvKws
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I have a very simple http resonse in my server where i json encode a struct. But its sending a blank of just {}
I don't know if i am doing it wrong but i get no errors. This is my json encode:
// Set uuid as string to user struct
user := User{uuid: uuid.String()}
fmt.Println(user) // check it has the uuid
responseWriter.Header().Set("Content-Type", "application/json")
responseWriter.WriteHeader(http.StatusCreated)
json.NewEncoder(responseWriter).Encode(user)
On the recieving end the data has:
Content-Type application/json
Content-Length 3
STATUS HTTP/1.1 201 Created
{}
Why does it not give me the uuid data? Am i doing something wrong with my encoding?
Export the field name by making the first character of the identifier's name a Unicode upper case letter (Unicode class "Lu").
Try this:
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
)
type User struct {
Uuid string
}
func handler(responseWriter http.ResponseWriter, r *http.Request) {
user := User{Uuid: "id1234657..."} // Set uuid as string to user struct
fmt.Println(user) // check it has the uuid
responseWriter.Header().Set("Content-Type", "application/json")
responseWriter.WriteHeader(http.StatusCreated)
json.NewEncoder(responseWriter).Encode(user)
}
func main() {
http.HandleFunc("/", handler) // set router
err := http.ListenAndServe(":9090", nil) // set listen port
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
output(http://localhost:9090/):
{"Uuid":"id1234657..."}
I have the following json data (from an external program, simplified a bit)
I can't change the json format.
[1416495600501595942, {"venue_id": 73, "message_type": "ABC", "sequence": 26686695}]
I'm having trouble unpacking it in Go, I think mostly because it's a list of disparate types.
The obvious thing to do seems to be to do is []interface{}, which works, but in converts it to a float64, which produces a roundoff error that I can't deal with (the number is a timestamp since epoch in nanos).
I can get it to work by unpacking it twice, as []interface{} and []int64, but that's obviously going to hinder performance, and I'm processing large amounts of data in real time.
I tried using struct here because that would get treated as a map, not a list[]
Is there any way to either pass it the format of the data, or make it default to int64 instead of float64? It's always going to be
[int64, map[string]interface{}]
ie I know the format of the upper level list, and that the keys of the map are strings, but the values could be anything (painfully, including decimals, which I think the only thing I can use to interpret them as is floats ...)
package main
import (
"encoding/json"
"fmt"
)
func main() {
j := `[1416495600501595942, {"venue_id": 73, "message_type": "ABC", "sequence": 26686695}]`
b := []byte(j)
fmt.Println(j)
var line []interface{}
var ints []int64
json.Unmarshal(b, &line)
fmt.Println(line)
// fmt.Println(line[0].(int64)) - this doesn't work
fmt.Println(line[0].(float64))
fmt.Println(int64(line[0].(float64)))
json.Unmarshal(b, &ints)
fmt.Println(ints)
}
Output is as follows:
[1416495600501595942, {"venue_id": 73, "message_type": "oKC", "sequence": 26686695}]
[1.416495600501596e+18 map[venue_id:73 message_type:oKC sequence:2.6686695e+07]]
1.416495600501596e+18
1416495600501595904
[1416495600501595942 0]
Solution (thanks to makpoc / dystroy)
package main
import (
"encoding/json"
"fmt"
"bytes"
)
func main() {
j := `[1416495600501595942, {"venue_id": 7.3, "message_type": "oKC", "sequence": 26686695}]`
b := []byte(j)
fmt.Println(j)
var line []interface{}
d := json.NewDecoder(bytes.NewBuffer(b))
d.UseNumber()
if err := d.Decode(&line); err != nil {
panic(err)
}
fmt.Println(line[0])
data := line[1].(map[string]interface{})
fmt.Println(data["venue_id"])
fmt.Println(data["sequence"])
}
According to this answer you can use Decoder -> and UseNumber or a struct instead of directly parsing the value.