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.
Related
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.
This question already has answers here:
How to "pretty" format JSON output in Ruby on Rails
(20 answers)
Closed 3 years ago.
I've run into a situation where I'm retrieving a json string in ruby, but I'm attempting to write it to a file structured like your usual json file (for example:
{
"top":{
"mid1":"bot1",
"mid2":"bot2"
}
}
However, the way the json string is structured is like so:
{"top":{"mid1":"bot1","mid2":"bot2"}}
I've seen other posts mention doing JSON.parse in order to get what I want, however that just causes it to look like this:
{"top"=>{"mid1"=>"bot1", "mid2"=>"bot2"}}
Is there a way that I can just convert the json string into what it looks like in the first code block above?
The first representation is JSON. The decoded version is Ruby's internal representation. If you want to go back to JSON, JSON.dump(...) will reconvert it.
Keep in mind this is just presentation. The data is the same.
You may be interested in the pp method as that produces nice, structured debugging output.
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 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
The json file I am calling can be found here. It contains population data for regions in Ghent (a city in Belgium) during a specific year. I use the following code to retrieve the data, the array where i store the response data is called "inwonersperwijk":
function(key,value) {
$.each(key,function(key,value){
inwonersperwijk.push(value);
});
$.each(key[0], function (key, value) {
years.push(key);
});
}
,
error: function(er){
console.log(er);
}});
which returns an object that looks like this.
What I am trying to do is retrieve the "wijk" in each of these objects. When I try to use inwonersperwijk[0].year_1999 for example, it returns the corresponding data just fine.
But when I want to retrieve the "wijk" part of the data by using inwonersperwijk[0].wijk, I get an undefined. Could anyone possibly help me out with this?
Looking at your JSON source file I can see that the indexes for the years are "year_1999", "year_2000" and so on, but the index for the wijk is actually "\ufeffwijk". The \ufeff is a non-breaking space (see here).
If you look at your output array screenshot, you will see that the year indices have no quotes, while the index for wijk does (it's "wijk" not wijk). This is why you get undefined: the index for wijk does not actually exist.
You're better off first filtering the \ufeff characters out of the JSON before parsing it.