Closed KoraiD closed 2 years ago
@hugovk, can you please help me to see if there is anything I should change in my request to get it merged?
Please could you start off by explaining why this change is needed and what you're expecting?
The bug report template may help us :)
Please include code that reproduces the issue.
The best reproductions are self-contained scripts with minimal dependencies.
# code goes here
Right, sorry that I missed the bug report template. :D
I used the Prettytable from_json()
function to parse a JSON file and print it as a table.
My expectation is that each element of the standard JSON object will be represented as individual lines.
The first element of the JSON object was used as a header, but got skipped in the iterator that converts the elements to rows. So, the first element of the JSON object was missing from the table.
Code that reproduces the issue:
from prettytable import from_json
import json
test_json = [
{"Name": "FirstEmployee", "Scores": 20000},
{"Name": "SecondEmployee", "Scores": 31000},
{"Name": "ThirdEmployee", "Scores": 14000}
]
table = from_json(json.dumps(test_json))
print(table)
Result:
+----------------+--------+
| Name | Scores |
+----------------+--------+
| SecondEmployee | 31000 |
| ThirdEmployee | 14000 |
+----------------+--------+
Expected result:
+----------------+--------+
| Name | Scores |
+----------------+--------+
| FirstEmployee | 20000 |
| SecondEmployee | 31000 |
| ThirdEmployee | 14000 |
+----------------+--------+
Solution:
prettytable.py
line 2404
def from_json(json_string, **kwargs):
table = PrettyTable(**kwargs)
objects = json.loads(json_string)
table.field_names = objects[0]
for obj in objects[0:]: # ---> changed from for obj in objects[1:]:
row = [obj[key] for key in table.field_names]
table.add_row(row)
return table
Thanks!
PrettyTable expects the JSON to start with the headers. For example add ["Name", "Scores"],
:
from prettytable import from_json
import json
test_json = [
["Name", "Scores"],
{"Name": "FirstEmployee", "Scores": 20000},
{"Name": "SecondEmployee", "Scores": 31000},
{"Name": "ThirdEmployee", "Scores": 14000}
]
table = from_json(json.dumps(test_json))
print(table)
+----------------+--------+
| Name | Scores |
+----------------+--------+
| FirstEmployee | 20000 |
| SecondEmployee | 31000 |
| ThirdEmployee | 14000 |
+----------------+--------+
This is useful as you can easily switch the order:
from prettytable import from_json
import json
test_json = [
["Scores", "Name"],
{"Name": "FirstEmployee", "Scores": 20000},
{"Name": "SecondEmployee", "Scores": 31000},
{"Name": "ThirdEmployee", "Scores": 14000}
]
table = from_json(json.dumps(test_json))
print(table)
+--------+----------------+
| Scores | Name |
+--------+----------------+
| 20000 | FirstEmployee |
| 31000 | SecondEmployee |
| 14000 | ThirdEmployee |
+--------+----------------+
Or just print out one:
from prettytable import from_json
import json
test_json = [
["Scores"],
{"Name": "FirstEmployee", "Scores": 20000},
{"Name": "SecondEmployee", "Scores": 31000},
{"Name": "ThirdEmployee", "Scores": 14000}
]
table = from_json(json.dumps(test_json))
print(table)
+--------+
| Scores |
+--------+
| 20000 |
| 31000 |
| 14000 |
+--------+
Got it, thanks for the detailed explanation!
You're welcome!
JSON should contain relevant data from the first object so the iterator should pick it up as a row and not just as the header.