Use this tag for questions related to the use of the encoding/json package of Go language.
Questions tagged [encoding-json-go]
20 questions
2
votes
1 answer
Go MarshalJSON behavior with multiple embedded structs
I'm testing out go json marshaling with embedded structs. However, I see that when I embed time.Time, why does it always override the other embedded structs even though they also provide their own custom marshaling? The code below always print out…

Le Hoang Long
- 428
- 3
- 10
2
votes
2 answers
Is it possible to have a structure for dynamic keys along with static keys for json in Golang
My apologies for the basic question. I am new to Golang and I have the json to parse as below
{
"config1":{
"Parameters":{
"Pm1":"value",
"Pm2":"value",
"Pm3":"value"
},
"dynamic_key1":{
…

hari hara
- 41
- 1
- 10
2
votes
1 answer
Go Gin converting json response to base64
I am trying to send db query data as json response . Here is my controller :
import (
"fmt"
"github.com/json-iterator/go"
"log"
)
func GetNewsPapers() []byte{
db := GetDB()
var json =…

pyprism
- 2,928
- 10
- 46
- 85
2
votes
1 answer
Parse json body array sent in POST request and print
I am having one issue while reading the json array. Need help for below query.
Request Json :
{ "httpReq": {
"username": "1234567890",
"password": "1234567890",
"number": "123456"
}
}
Response Json :
{ "httpResp": {
"status": "Pass",
…

Quick learner
- 699
- 4
- 22
- 39
1
vote
1 answer
Render table in html template where table-data are keys and values of some json data in Golang
In backend I send http request to some third party site and retrieve some json data in response. Keys in the json response are not always same, but I have some idea of what they might be. For example:
Sending request to example.com/data/1 could…

skylark
- 11
- 3
1
vote
1 answer
parse nested json using struct in go language
Unable to parse nested json into structs objects using go lang
I have a nested json string that I want to parse using struct in Go language. The json looks like…

Byte
- 37
- 4
1
vote
0 answers
Golang unmarshall nested dynamic JSON object in struct
I have a JSON in the following format:
{
"fieldA": {
"dynamicField": "string value",
"moreDynamicField": ["or", "array", "of", "strings"],
"allFieldsAreDynamic": "values in str or arr[str] only"
},
"fieldB": {
"dynamicField":…

fhfuih
- 149
- 2
- 11
1
vote
5 answers
Prepare a json object from unmarshaled data
I have json data like this:
json: {"opt1":200,"opt3":"1","opt4":"13","opt5":null,"products":[{"product_id":1,"price":100,"variant_id":100},{"product_id":1,"price":100,"variant_id":null}]}
I have structured it using
type Products struct {
…

Animesh
- 417
- 2
- 5
- 14
1
vote
1 answer
How to convert a deeply nested part of json into a single string with Go
I have some json data that has a structure similar to the following:
{
"value1": "some value"
"value2": "some other value"
"value3": "another value"
"value4": {
"data":[
{
...more nested…

user1898662
- 65
- 3
- 12
1
vote
3 answers
How to decode JSON in Go which returns multiple elements as array of type and individual elements as type
I am working with an API that sends JSON data. The problem is that an array of a single element shows up as a single value. For example, consider the following JSON:
{ "names": ["Alice","Bob"] }
The API sends this as an array. But when the names…

Teodor Maxim
- 471
- 6
- 9
1
vote
3 answers
how to unmarshal json object if object is returning as empty string instead of empty struct
I'm receiving some data as JSON, but if a object is empty, it does not return a empty struct but a empty
string instead, and when unmarshaling, it returns an error.
So instead of data being {"key":{}} is {"key":""}} , it does not work even using…

John Balvin Arias
- 2,632
- 3
- 26
- 41
0
votes
0 answers
How to write secret object fetched from Goclient for kubernetes
I have
func (ss *K8sService) GetSecret(k8sclient kubernetes.Interface, namespace, secretName string) (secret *corev1.Secret, err error) {
secret, err = k8sclient.CoreV1().Secrets(namespace).Get(context.TODO(), secretName,…

ambikanair
- 4,004
- 11
- 43
- 83
0
votes
1 answer
Custom JSON Marshaller Supporting Base64 encoding | error calling MarshalJSON for type routes.Temp: invalid character 'e'
I want to write a custom Marshaller. I have done the implementations as Following.
type Temp struct {
Time time.Time
}
func (t Temp) MarshalJSON() ([]byte, error) {
type __ Temp
var x = __(t)
var buff bytes.Buffer
if err :=…

vkstack
- 1,582
- 11
- 24
0
votes
1 answer
Exporting JSON into single file from loop function
I wrote some code which hits one public API and saves the JSON output in a file. But the data is storing line by line into the file instead of a single JSON format.
For eg.
Current Output:
{"ip":"1.1.1.1", "Country":"US"}
{"ip":"8.8.8.8",…

Wr3nch0x1
- 13
- 3
0
votes
1 answer
How to customize indent function during json.Marshal?
We have designed an "export" API which enables users to download a json file with information. The json is an array. Now we meet a little dilemma.
Call json.Marshal directly (no indent, not so user-friendly)
[{"foo":"bar"},{"foo1":"bar1"}]
Call…

Flying onion
- 126
- 7