Parse a JSON Object - ruby

I have a JSON object like the following:
{
"a" : "test",
"b" : "test",
"c" : [{"d": "test data", "f": ['e','f','g']},
{"f": "test data", "f": ['e','f','g']}
],
"d" : [{"g": "test data", "f": ['e','f','g']},
{"h": "test data", "f": ['e','f','g']}
]
}
I need to parse this and display it in a tree format. Could somebody show some direction please. I am working on ruby.

For a JSON parser, look at JSON.org
Listed there:
json
yajl-ruby
json-stream

Related

How to convert this complex JSON into spring boot and store in sql server

[
{
"Name": "xyz",
"age": "20",
"socialMedia": [
{
"fb": "aaa"
},
{
"insta": "bbb"
}
],
"languge": "eng",
"country": "USA",
"Experiance": [
{
"developer": {
"companyName": "abcd",
"years": "5"
},
"tester": {
"companyName": "efgh",
"years": "3"
}
}
]
}
]
i am getting this json from front-end and i need to convert this json into spring-boot code and store in H2(in-memory) database......
intentionally i am not sharing my code.....
your answer might help me to enrich my code ...
so, please try to give your best using advanced concepts in spring-boot
Thanks In Advance
Here, you can look about JSON data types: JSON data types
The link includes that how data types present JSON.
Then you can configure object model for JSON data.

Can I use for loop in karate?

I am using get request for getting json response. And it is dynamic. My response looks like this.
{
"data": [
{
"id": 1,
"name": "Jack",
"gender": "male"
},
{
"id": 2,
"name": "Jill",
"gender": "female"
}
]
}
Can I use id from this response and put that id in 'for loop' to execute delete method?
Given path 'profile/delete/id'
And if I can then how?
Yes, refer the docs: https://github.com/intuit/karate#data-driven-features
* def result = call read('delete.feature') response.data
And in delete.feature you will be able to refer to the id variable directly.

Python convert Json string to for each

I'm reading a google spreadsheet with gspread using the sheet.get_all_records(), it outputs the following:
[{"name": "test 0", "name1": "test 1", "name2": "test 2", "name3": "test 3"}, {"name": "test 0", "name1": "test 1", "name2": "test 2", "name3": "test 3"}, {"name": "test 0", "name1": "test 1", "name2": "test 2", "name3": "test 3"}, {"name": "test 0", "name1": "test 1", "name2": "test 2", "name3": "test 3"}]
I like to treat each row separatly:
list_of_hashes = sheet.get_all_records()
json_obj = json.dumps(list_of_hashes)
for line in json_obj:
print (line['name'])
print (line['name1'])
i get the error
TypeError: string indices must be integers
like it's not recognizing the Json
any idea how to solve this
How about a following modification?
json.dumps() is used for retrieving a JSON object as a string. I think that in order to modify your script, there are following 2 patterns.
Pattern 1:
From :
json_obj = json.dumps(list_of_hashes)
To :
json_obj = json.loads(json.dumps(list_of_hashes))
Pattern 2:
If sheet.get_all_records() returns a JSON object, you can directly use the returned values to for loop as follows.
json_obj = sheet.get_all_records()
for line in json_obj:
print (line['name'])
print (line['name1'])
If I misunderstand your question, I'm sorry.

Ruby - how to convert a large, multi-dimensional, array of hashes to CSV format

I have a quite large array of hashes (stored in "#hash["response"]["results"])" returned by my program in JSON format.
I have seen several examples on Stack Overflow on how to convert a simple hash to CSV format, however I haven't been able to find any complex examples of doing it with a larger dataset.
I would like to use the hash keys ("pluginID", "ip", "pluginName", etc.) as the CSV headers and the hash values ("11112", "100.100.100.100", "Name for plugin here", etc.) for the CSV row content.
Note that the "repository" key is a hash itself and for that I'd like to just use the name, as opposed to the ID or description.
Any help is greatly appreciated. I have played with some code samples following the Ruby CSV standard library instructions but I am not even getting close.
#hash = '{
"type": "regular",
"response": {
"Records": "137",
"rRecords": 137,
"startOffset": "0",
"endOffset": "500",
"matchingDataElementCount": "-1",
"results": [
{ "pluginID": "11112",
"ip": "100.100.100.100",
"pluginName": "Name for plugin here",
"firstSeen": "1444208776",
"lastSeen": "1451974232",
"synopsis": "synopsis contents",
"description": "Full description would go here... Full description would go here... Full description would go here... Full description would go here... Full description would go here...",
"solution": "",
"version": "Revision: 1.51",
"pluginText": "output text here",
"dnsName": "name",
"repository": {
"id": "1",
"name": "Name Here As Well",
"description": "Description here also"
},
"pluginInfo": "11112 (0/6) Name for plugin here"
},
{ "pluginID": "11113",
"ip": "100.100.100.100",
"pluginName": "Name for plugin here",
"firstSeen": "1444455329",
"lastSeen": "1451974232",
"synopsis": "Tsynopsis contents",
"description": "Full description would go here... Full description would go here... Full description would go here... Full description would go here... Full description would go here...",
"solution": "",
"version": "Revision: 1.51",
"pluginText": "output text here",
"dnsName": "name here",
"repository": {
"id": "1",
"name": "Name Here As Well",
"description": "Description here also"
},
"pluginInfo": "11112 (0/6) Name for plugin here"
},
{ "pluginID": "11113",
"ip": "100.100.100.100",
"pluginName": "Name for plugin here : Passed",
"firstSeen": "1444455329",
"lastSeen": "1444455329",
"synopsis": "nope, more synopsis data here",
"description": "Uanother different description",
"solution": "",
"version": "Revision: 1.14",
"pluginText": "",
"dnsName": "name here",
"repository": {
"id": "1",
"name": "Name Here As Well",
"description": "Description here also"
},
"pluginInfo": "11114 (0/6) Name for plugin here : Passed"
},
{ "pluginID": "11115",
"ip": "100.100.100.100",
"pluginName": "Name for plugin here",
"firstSeen": "1444455329",
"lastSeen": "1444455329",
"synopsis": "Tsynopsis contents",
"description": "Full description would go here... Full description would go here... Full description would go here... Full description would go here... Full description would go here...",
"solution": "",
"version": "Revision: 1.51",
"pluginText": "output text here",
"dnsName": "",
"repository": {
"id": "1",
"name": "Name Here As Well",
"description": "Description here also"
},
"pluginInfo": "11116 (0/6) Name for plugin here"
}
]
},
"code": 0,
"msg": "",
"msg_det": [],
"time": 1454733549
}'
This is pretty easy. There are essentially five steps:
Parse the JSON into a Ruby Hash.
Get the key names from the first hash in the "results" array† and write them to the CSV file as headers.
Iterate over the "results" array and for each hash:
Replace the "repository" hash with its "name" value.
Extract the values in the same order as the headers and write them to the CSV file.
The code looks something like this:
require 'json'
require 'csv'
json = '{
"type": "regular",
"response": {
...
},
...
}'
# Parse the JSON
hash = JSON.parse(json)
# Get the Hash we're interested in
results = hash['response']['results']
# Get the key names to use as headers
headers = results[0].keys
filename = "/path/to/output.csv"
CSV.open(filename, 'w', headers: :first_row) do |csv|
# Write the headers to the CSV
csv << headers
# Iterate over the "results" hashes
results.each do |result|
# Replace the "repository" hash with its "name" value
result['repository'] = result['repository']['name']
# Get the values in the same order as the headers and write them to the CSV
csv << result.values_at(*headers)
end
end
†This code (headers = results[0].keys) assumes that the first "results" hash will have all of the keys you want in the CSV. If that's not the case you need to either:
Specify the headers explicitly, e.g.:
headers = %w[ pluginId ip pluginName ... ]
Loop over all of the hashes and build a list of all of their keys:
headers = results.reduce([]) {|all_keys, result| all_keys | result.keys }
I used solution like it:
stats_rows = #hash["responce"]["results"].each_with_object([]) do |e, memo|
memo << [e["pluginID"], e["ip"], e["pluginName"]]
end
CSV.generate do |csv|
csv << ["pluginID", "ip", "pluginName"] #puts your hash keys into SCV
stats_rows.each do |row| #values
csv << row
end
end

Additional info in JSON for jQuery dataTable plugin

jQuery dataTable plugin expects the data (aaData) in a format where aaData is an array of array.Like below
{ "aaData":[
[
"1",
"2, 3",
"4",
"5"
] ] }
Now if I want to send some additional information in my json to make some disicisions like to make the contents of second column a hyperlink or nor.How to do that ?
Did you try add your own key into JSON?
{ "aaData":[ [ "1", "2, 3", "4", "5" ] ], "mydata":{"keyN":1, "keyM": [1,2,3,4] } }

Resources