Python convert Json string to for each - gspread

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.

Related

join eloquent table with custom format response

I'm just join 3 eloquent tables. it works but the response json format is not what I want. I need to customizing the format response so it will easy to read.
this is my join table code
$showPresensi = Event::join('presensis', 'presensis.event_id', '=', 'events.id')
->join('anggotas', 'presensis.anggota_id', '=', 'anggotas.id')
->where('presensis.event_id', 2)
->get(['events.*', 'anggotas.nama_lengkap','presensis.kehadiran']);
return $this->ok($showPresensi, "Success");
and this is my current response json
{
"message": "Success",
"success": true,
"code": 200,
"data": [
{
"id": 2,
"nama": "Event 2",
"tanggal": "Sabtu, 12 Februari 2022",
"permintaan_dari": "Yang bersangkutan",
"hasil_kegiatan": "nothing",
"created_at": "2022-02-12T21:53:14.000000Z",
"updated_at": "2022-02-12T21:53:14.000000Z",
"nama_lengkap": "user 1",
"kehadiran": "0"
},
{
"id": 2,
"nama": "Event 2",
"tanggal": "Sabtu, 12 Februari 2022",
"permintaan_dari": "Yang bersangkutan",
"hasil_kegiatan": "nothing",
"created_at": "2022-02-12T21:53:14.000000Z",
"updated_at": "2022-02-12T21:53:14.000000Z",
"nama_lengkap": "user 3",
"kehadiran": "0"
},
{
"id": 2,
"nama": "Event 2",
"tanggal": "Sabtu, 12 Februari 2022",
"permintaan_dari": "Yang bersangkutan",
"hasil_kegiatan": "nothing",
"created_at": "2022-02-12T21:53:14.000000Z",
"updated_at": "2022-02-12T21:53:14.000000Z",
"nama_lengkap": "user 2",
"kehadiran": "1"
}
]
}
I want to column of nama_lengkap and kehadiran become an array so it will become like this:
{
"message": "Success",
"success": true,
"code": 200,
"data": [
{
"id": 2,
"nama": "Event 2",
"tanggal": "Sabtu, 12 Februari 2022",
"permintaan_dari": "Yang bersangkutan",
"hasil_kegiatan": "nothing",
"created_at": "2022-02-12T21:53:14.000000Z",
"updated_at": "2022-02-12T21:53:14.000000Z",
"presensi": [
{
"nama_lengkap": "user 1",
"kehadiran": "0"
},
{
"nama_lengkap": "user 3",
"kehadiran": "0"
},
{
"nama_lengkap": "user 2",
"kehadiran": "1"
}
]
}
]
}
is there any solution for this? any clues are helps. thank you
The easiest way would be to use nested eager loading with Eloquent.
Then you could do:
$showPresensi = Event::with('presensis', 'presensis.anggotas').find(2);
The structure should match more closely what you're going for. Then you could either modify the collection to get exactly what you want, or add accessors to your models.

Creating Test in Jira using Rest Assured

I am new to Rest Assured and currently trying to create a JSON message for posting an issue of type TEST in Jira using Rest Assured. However, I am not able to properly create the test steps in the message. Below are the code and the message structure that I am getting.
TestStepMap teststep = new TestStepMap();
List<TestStepMap> s = new ArrayList<TestStepMap>();
for (int i=0; i<4; i++)
{
int index = i;
String step = "Step " + (i+1);
String data = "Data " + (i+1);
String result = "Result " + (i+1);
teststep.setIndex(index);
teststep.setStep(step);
teststep.setData(data);
teststep.setResult(result);
s.add(i, teststep);
}
CustomField10011Map customfield_10011 = new CustomField10011Map();
customfield_10011.setSteps(s);
This is the output that I am getting.
{
"fields": {
"project": {
"key": "RT"
},
"summary": "Sum of two numbers",
"description": "example of manual test",
"issuetype": {
"name": "Test"
},
"customfield_10007": {
"value": "Manual"
},
"customfield_10011": {
"steps": [
{
"index": 3,
"step": "Step 4",
"data": "Data 4",
"result": "Result 4"
},
{
"index": 3,
"step": "Step 4",
"data": "Data 4",
"result": "Result 4"
},
{
"index": 3,
"step": "Step 4",
"data": "Data 4",
"result": "Result 4"
},
{
"index": 3,
"step": "Step 4",
"data": "Data 4",
"result": "Result 4"
}
]
}
}
}
The test steps 1, 2 and 3 are being overwritten by the last step. How can I solve the issue? Any suggestions are highly welcome.
Thanks in advance.
You're adding the same teststep instance to list s every time.
What you want is to create and add a new TestStepMap object per iteration, otherwise you'll always be overwriting updates on that object from an earlier iteration of the loop.
List<TestStepMap> s = new ArrayList<TestStepMap>();
for (int i=0; i<4; i++)
{
int index = i;
String step = "Step " + (i+1);
String data = "Data " + (i+1);
String result = "Result " + (i+1);
TestStepMap teststep = new TestStepMap();
teststep.setIndex(index);
teststep.setStep(step);
teststep.setData(data);
teststep.setResult(result);
s.add(i, teststep);
}

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

How to create non-hierarchical relationship circular diagram with D3.js?

I'm trying to create a diagram which shows links between nodes on the same hierarchy level but witthin different groups in a circular layout. It's basically a visualisation of keywords/tags/groups attached to objects a la Circos:
var data =
[
{
"name": "Adam",
"tags": ["tall", "blonde"]
},
{
"name": "Barbara",
"tags": ["tall", "brunette", "student", "single"]
}
];
Or, like something I'm trying to do:
var data =
[
{
"name": "Project 1",
"categories":
[
"Category A",
"Category B",
"Category C",
"Category D"
]
},
{
"name": "Project 2",
"categories":
[
"Category C",
"Category D",
"Category E"
]
},
{
"name": "Project 3",
"categories":
[
"Category A",
"Category E"
]
},
{
"name": "Project 4",
"categories":
[
"Category A",
"Category C",
"Category D",
"Category E"
]
},
{
"name": "Project 5",
"categories":
[
"Category A",
"Category B",
"Category D"
]
}
];
Each object can have a number of categories corresponding to the number of node copies appearing in the circle. Visually, it should look something like this:
Is it possible with D3.js? If so, which layout? It looks like it would be something between chord (but without values) and bundle (without hierarchy).
I'd imagine the script would need to traverse through the JSON data and find all unique categories first and then distribute and interlink nodes of each object depending on what and how many categories are attached to it. Finally, it would label each category.
Any ideas would be appreciated.

Parse a JSON Object

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

Resources