How to append JSON into JSON?


(Ήαϻe̸d̨ MΌsϮαϝαe̸ΐ༄) #1

Hi there

can’t we push JSON into JSON?

playGround : https://play.golang.org/p/HSE46L9h_Yb

package main

import (
	"encoding/json"
	"github.com/emirpasic/gods/maps/linkedhashmap"
)
func GetGames(){
	type GameDetails struct {
		Name         string   `json:"nm"`
		Id                int       `json:"id"`
		ThumbUrl   string  `json:"tmb_ul"`
		Requirement   linkedhashmap.Iterator  `json:"reqs,omitempty"`
	}

	games := new(GameDetails)

	games.Name = "Stronghold Crusader"
	games.Id =1120
	games.ThumbUrl="picture/eeee.jpg"



	//this is JSON in database:
	requirement:=`{"Hard disk space":"3 GB","Memory":"768 MB","Operating system":"Windows XP/Vista/7","Processor":"Intel Pentium III 700 MHz or AMD Athlon 500 MHz K6","Sound device":"compatible with DirectX","Video card":"128 MB (GeForce 5700/Radeon 9600)"}`


	n := linkedhashmap.New()
	err:=n.FromJSON([]byte(requirement))
	if err != nil {
		print(err)
	}
	result:=n.Iterator()
	games.Requirement=result

	mkJson,err:= json.Marshal(games)
	if err != nil {
	print(err)
	 }
	print(string(mkJson))
}

I have tried this way but doesn’t work.(linkedhashmap)

now, how can append “requirement” to games?
1-unmarshal to map[string]string doesn’t work, Because I want JSON as it is
-we haven’t sort map from Golang version 1 and later
2-unmarshal to struct doesn’t work too because we can’t add automatically struct field if an added field in “requirement” JSON

Thanks in advance.


(Johan Dahl) #2

Hi

You could do like this: https://play.golang.org/p/PzN7nByVbag

package main

import (
	"encoding/json"
)

func main() {
	GetGames()
}
func GetGames() {
	type GameDetails struct {
		Name          string `json:"nm"`
		Id            int    `json:"id"`
		ThumbUrl      string `json:"tmb_ul"`
		HardDiskSpace string `json:"Hard disk space"`
		Memory        string `json:"Memory"`
		OperatingSystem string `json:"Operating system"`
	}

	games := new(GameDetails)

	games.Name = "Stronghold Crusader"
	games.Id = 1120
	games.ThumbUrl = "picture/eeee.jpg"

	//this is JSON in database:
	requirement := `{"Hard disk space":"3 GB","Memory":"768 MB","Operating system":"Windows XP/Vista/7","Processor":"Intel Pentium III 700 MHz or AMD Athlon 500 MHz K6","Sound device":"compatible with DirectX","Video card":"128 MB (GeForce 5700/Radeon 9600)"}`

	err := json.Unmarshal([]byte(requirement), games)
	if err != nil {
		print(err)
	}

	mkJson, err := json.Marshal(games)
	if err != nil {
		print(err)
	}
	print(string(mkJson))
}

(Ήαϻe̸d̨ MΌsϮαϝαe̸ΐ༄) #3

thanks, but what will happen if we add an aditional field in json
and not in code? like this :
{"shader":3 ,"Hard disk space":"3 GB","Memory":"768 MB","Operating system":"Windows XP/Vista/7","Processor":"Intel Pentium III 700 MHz or AMD Athlon 500 MHz K6","Sound device":"compatible with DirectX","Video card":"128 MB (GeForce 5700/Radeon 9600)"}


(Johan Dahl) #4

Hi. They will be ignored. But you could also do like this. https://play.golang.org/p/JjasqJEVDKk

package main

import (
	"encoding/json"
)

func main() {
	GetGames()
}
func GetGames() {
	type GameDetails struct {
		Name        string            `json:"nm"`
		Id          int               `json:"id"`
		ThumbUrl    string            `json:"tmb_ul"`
		Requirement map[string]string `json:"reqs,omitempty"`
	}

	games := new(GameDetails)

	games.Name = "Stronghold Crusader"
	games.Id = 1120
	games.ThumbUrl = "picture/eeee.jpg"

	//this is JSON in database:
	requirement := `{"Hard disk space":"3 GB","Memory":"768 MB","Operating system":"Windows XP/Vista/7","Processor":"Intel Pentium III 700 MHz or AMD Athlon 500 MHz K6","Sound device":"compatible with DirectX","Video card":"128 MB (GeForce 5700/Radeon 9600)"}`

	err := json.Unmarshal([]byte(requirement), &games.Requirement)
	if err != nil {
		print(err)
	}

	mkJson, err := json.Marshal(games)
	if err != nil {
		print(err)
	}
	print(string(mkJson))
}


(Boban Acimovic) #5

https://golang.org/pkg/encoding/json/#RawMessage


(Ήαϻe̸d̨ MΌsϮαϝαe̸ΐ༄) #6

thanks, but this didn’t work,
I want preserved order map


(Johan Dahl) #7

I’m just curious. Why is order important? http://www.json.org/ states the following

An object is an unordered set of name/value pairs. An object begins with { (left brace) and ends with } (right brace). Each name is followed by : (colon) and the name/value pairs are separated by , (comma).


(Norbert Melzer) #8

There is no order in a map, neither in go, nor in JSON.


(Ήαϻe̸d̨ MΌsϮαϝαe̸ΐ༄) #9

thanks acim, u completely solved my problem.

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