Ruby Regex for Json in Chef Recipe [duplicate] - json

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"

Related

Convert one line json into multiple lines to write to a file (Ruby) [duplicate]

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.

Substitute list of strings in JSON file [duplicate]

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

Mongodb/Mongo Query: How do I deserialize nested JSON stored in a string field? [duplicate]

This question already has answers here:
MongoDB: How to query over a json string?
(1 answer)
Using stored JavaScript functions in the Aggregation pipeline, MapReduce or runCommand
(1 answer)
Closed 3 years ago.
In the context of metabase:
I am applying the following one-liner query to extract a particular field from a collection of mongodb documents:
[{"$project":{"myName":"$field1.field2" }},
{"$match":{"_id":{"$eq":"blah"}}} ]
myName is a string field that is in fact a serialized JSON object.
How can I adapt the pipeline above, perhaps using JSON.parse(), to pull out fields nested further down the JSON hierarchy?
Part II: How would I add a final step to extract some of those deeply nested fields? The situation is further complicated because the document structure is not consistent between documents...
Thanks!

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.