jazzband / prettytable

Display tabular data in a visually appealing ASCII table format
https://pypi.org/project/PrettyTable/
Other
1.36k stars 155 forks source link

Fixed JSON object #198

Closed KoraiD closed 2 years ago

KoraiD commented 2 years ago

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.

KoraiD commented 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?

hugovk commented 2 years ago

Please could you start off by explaining why this change is needed and what you're expecting?

The bug report template may help us :)

What did you do?

What did you expect to happen?

What actually happened?

What versions are you using?

Please include code that reproduces the issue.

The best reproductions are self-contained scripts with minimal dependencies.

# code goes here
KoraiD commented 2 years ago

Right, sorry that I missed the bug report template. :D

What did you do?

I used the Prettytable from_json() function to parse a JSON file and print it as a table.

What did you expect to happen?

My expectation is that each element of the standard JSON object will be represented as individual lines.

What actually happened?

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.

What versions are you using?

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
hugovk commented 2 years ago

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  |
+--------+
KoraiD commented 2 years ago

Got it, thanks for the detailed explanation!

hugovk commented 2 years ago

You're welcome!