Substitute list of strings in JSON file [duplicate] - json

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).

Related

Encode json array to client [duplicate]

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.

Ruby Regex for Json in Chef Recipe [duplicate]

This question already has answers here:
Parsing a JSON string in Ruby
(8 answers)
Closed 4 years ago.
From zabbix-api login I have this output:
{"jsonrpc":"2.0","result":"4b79a399f043fa44c5653bee3ecb346d","id":0}
I'm trying to parse using the code above in ruby:
command_out = shell_out(command).stdout.to_s
node.default['zabbix_server']['zabbix_auth'] = command_out.lines.grep(/"(result)":"((\\\"|[^"])*)"/)
how can I grab only the "4b79a399f043fa44c5653bee3ecb346d" ?
You are actually handling a JSON, which is a data format like XML or YAML.
Luckily enough, you can rely on the JSON gem, developed to parse a standard JSON.
Here is how you can use it:
require 'JSON'
my_json = '{"jsonrpc":"2.0","result":"4b79a399f043fa44c5653bee3ecb346d","id":0}'
a = JSON.parse(my_json)
p a['result']
and the output is:
"4b79a399f043fa44c5653bee3ecb346d"

Jade/Pug iteration over data from server is massively over counted [duplicate]

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.

Multiple Lines in Json file [duplicate]

This question already has answers here:
pretty-print JSON using JavaScript
(31 answers)
Closed 5 years ago.
I am storing some query in json file. the query is little bit lengthy. I want to store these query in multiple lines to show query clearly. In C# when we have like this query we put in the front of query # .. Is there any special key to put in the front of json string?
below is image what I mean by this.
No, there is no way to do this; newlines are not allowed in JSON strings [1].

How to get string in JSON? [duplicate]

This question already has answers here:
Jmeter extracting fields/parsing JSON response
(6 answers)
Closed 6 years ago.
I have the JSON like this:
{"message":"[{\"file_name\":\"1464312906_84174_upload.jpg\",\"file_url\":\"uploads\\/tmp\\/1464312906_84174_upload.jpg\"}]","code":200}
How to get the string "1464312906_84174_upload.jpg" by JSON Expression?
message appears to be a JSON-encoded array. So parse it and then access the property you want.
JSON.parse(data.message)[0].file_name
Replace data with the variable containing the object you showed in the question.