This question already has answers here:
MarshalJSON without having all objects in memory at once
(2 answers)
Closed 2 years ago.
I have the opposite problem as this question.
Decode large stream JSON
In that question, the user asks about decoding a large incoming JSON array.
But, how would I encode a large outgoing JSON array?
For example, I have a http.Handler like this.
enc := json.NewEncoder(resp)
for obj := range objectChannel {
enc.Encode(obj)
}
However, this doesn't work because it ends up sending invalid JSON to the JavaScript client.
Do I have to manually fix the syntax? For example:
enc := json.NewEncoder(resp)
fmt.Fprint(resp, "[")
for obj := range objectChannel {
enc.Encode(obj)
fmt.Fprint(resp, ",") // and account for the last item
}
fmt.Fprint(resp, "]")
Or is there a better way?
If you already have all the objects in memory, there is no point in encoding elements one by one, and you can do:
enc.Encode(lotsOfObjs)
If you get objects one by one, then manually adding [ and ] with commas in between is fine.
Related
This question already exists:
How can I extract JSON Schema from a given JSON in Go? [closed]
Closed 3 years ago.
Is there a way that to convert a JSON to its schema in Go? I need to compare 2 JSON templates or schemas and cannot find any package or function to do the same - can someone please help me with this?
You can have a look at gjson library. It has functions to parse and get unmarshalled JSON. You can use gjson functionality to compare json results.
I think you will need to unmarshal them recursively (if they contain nested json) into something like map[string]interface{}, and then loop through and compare the keys. There are some libraries mentioned on this question https://stackoverflow.com/a/42153666 which could be used to unmarshal them safely.
For example, You can use Exists from the gabs library while iterating through the keys in the unmarhsalled map to see if the same keys exist in the other map.
// From gabs library
// Exists checks whether a field exists within the hierarchy.
func (g *Container) Exists(hierarchy ...string) bool {
return g.Search(hierarchy...) != nil
}
Edit: without libraries here: https://play.golang.org/p/jmfFsLT0G1n based on the test case of this code golf exercise: https://codegolf.stackexchange.com/questions/195476/extract-all-keys-from-an-object-json
The json package provided in Go’s standard library provides us with all the functionality we need. For any JSON string, the standard way to parse it is:
import "encoding/json" //...
// ... myJsonString := `{"some":"json"}`
// `&myStoredVariable` is the address of the variable we want to store our // parsed data in
json.Unmarshal([]byte(myJsonString), &myStoredVariable)
//...
This question already has answers here:
Re.sub not working for me
(2 answers)
Closed 3 years ago.
I'm trying to substitute a list of Json strings from a dumped json file.
to_clean_up = [web-app,servlet-one,init-param, servlet-mapping]
stringify = json.dumps(data)
for i in to_clean_up:
regex = re.sub(r'[^\w]','_', i)
for m in stringify:
m.replace(i,regex)
So im trying to substitute there words in the stringify , but, my stringify somehow stays the same.
According to the docs on str.replace, this method returns a new string without modifying the original.
You should do something like this:
for j, m in enumerate(stringify):
stringify[i] = m.replace(i, regex)
Moreover, in this loop m will be a single character, so I'm not really sure what you're trying to do here... You could as well do stringify = stringify.replace(i, regex).
This question already has an answer here:
Can't parse JSON array with JSONDecoder in Swift 4
(1 answer)
Closed 4 years ago.
Some context: reddit post
I have two structs. One is called bio that has fields such as name, bio, spotifyURL, etc. The other struct is called Artists which has one field, bios, an array of bio.
My JSON file has an array as the root element, with two elements (artists) inside it. The identifiers of the elements match up with the identifiers of the fields in the structs.
When I call:
let bioArray = try
JSONDecoder().decode(Artists.self, from: data)
bioArray.bios is an empty array. Shouldn't this populate the array? The data was retrieved successfully, decode() just isn't doing what I think it should be doing. Thanks
EDIT: Wow, I fixed it. In Artists I had:
let bios:[bio] = []
I changed it to:
let bios:[bio]
and now it works...
Wow, I fixed it. In Artists I had:
let bios:[bio] = []
I changed it to:
let bios:[bio]
and now it works...
The answer is in the duplicate link but I'll put it here too:
To fix this either remove the initialization (like I did) or change the let to var.
This question already has answers here:
Golang parse JSON array into data structure
(3 answers)
Closed 5 years ago.
Currently I have stored in my database json objects as string. I want to pass them to a map to be able to consult any field as:
Mymap["Name"]
Mymap["Age"]
..
Let's say that my string would be something like:
'{"Name":["zero"],"Age":"10"}'
I don't know the structure of the data, so that Json can have many fields as required and also many levels of nestings (but I am worried more about to get at least the first level)
If you're dealing with a json object of arbitrary structure you can use a map of interfaces as the type to unmarshal it into.
map[string]interface{}
The encoding/json package will nicely unmarshal the json object into it, nested or not.
This, while very flexible, has an obvious downside, the types of the map's values are unknown and so to do anything useful with them you'll have to use a type assertion or type switch.
v, ok := m["key"].(Type)
https://play.golang.org/p/wM0gkU1g5G
This question already has answers here:
how to render json object in jade and loop through results
(3 answers)
Closed 5 years ago.
I'm trying to iterate over a json array of 3 objects in Jade in order to populate some html (3 is arbitrary, I have one user with 380+).
Instead of 3 divs, I'm getting about 5,000+. How do I iterate over this object from my Node.js sever so it will render properly in Jade/Pug? My guess is that, instead of giving me the length of the array (3), it's giving me the length of the json string that makes that array which is over 5,000.
My server is sending this:
res.render('yourUploads', {fromServer:JSON.stringify(rows[0])});
rows[0] is the results from a mysql database query, 3 results.
script.
var data = !{fromServer};
console.log(data); //prints "(3) [Object, Object, Object]"
console.log(data.length); //prints "3"
mixin posMixin(uploadData)
div #{uploadData}
body
div Welcome to your awesome uploads page
div(onclick="window.location='/createNewUpload'") go to your awesome Create New Upload page
- for (i = 0; i < fromServer.length; ++i) { //fromServer.length is about 5,000?
+posMixin("whateverItTakesToGetThisToWork") //puts 5,000 divs into the dom!!!
- }
As you might have guessed, I'm not exactly sure of what to put in place of "whateverItTakesToGetThisToWork", but I wouldn't be surprised if it became rather obvious after I figure out what's wrong with this fromServer object. I had thought it would be fromSever[i] but that produces output so large it crashes my machine.
Because you serielize the data using JSON.stringify, now the fromServer is a string, and maybe you cannot use the JSON methods in pug, just don't use JSON.stringify, pass the data directly.