-1

My function

def string_info(json_info, string):
    if type(json_info) == dict:
        for key in json_info.keys():
            other = string_info(json_info[key], "")
            string += f"\n{key}:\t{other}"
    elif type(json_info) == list:
        for index in range(0, len(json_info)):
            string += f"{json_info[index]} "
    else:
        string += f"{json_info}"
    return string

Sample JSON

"statistics": {
        "level": {
            "current": 100,
            "progress": 52
        },
        "pp": 5934,
        "pp_rank": 15496,
        "ranked_score": 15283968302,
        "hit_accuracy": 98.3355,
        "play_count": 76169,
        "play_time": 3855235,
        "total_score": 79160699555,
        "total_hits": 13126452,
        "maximum_combo": 2104,
        "replays_watched_by_others": 24,
        "is_ranked": true,
        "grade_counts": {
            "ss": 51,
            "ssh": 22,
            "s": 1202,
            "sh": 224,
            "a": 1272
        },
        "rank": {
            "global": 15496,
            "country": 2553
        }
    }

My Output


level:  
current:    100
progress:   52
pp: 5934
pp_rank:    15496
ranked_score:   15283968302
hit_accuracy:   98.3355
play_count: 76169
play_time:  3855235
total_score:    79160699555
total_hits: 13126452
maximum_combo:  2104
replays_watched_by_others:  24
is_ranked:  True
grade_counts:   
ss: 51
ssh:    22
s:  1202
sh: 224
a:  1272
rank:   
global: 15496
country:    2553

It seems like the tabs are not the same size for every line and I was expecting an output more along the lines of


level:  
   current:   100
   progress:   52
pp:   5934
pp_rank:   15496
ranked_score:   15283968302
hit_accuracy:   98.3355
play_count:   76169
play_time:   3855235
total_score:   79160699555
total_hits:   13126452
maximum_combo:   2104
replays_watched_by_others:   24
is_ranked:   True
grade_counts:   
   ss:   51
   ssh:   22
   s:   1202
   sh:   224
   a:   1272
rank:   
   global:   15496
   country:   2553

I'm pretty new to programming with python, and I was wondering what mistake I have made to have inconsistent tabs and having the spacing not as I expected in the output. Thanks for any help.

(Also, the if statement for the if is for other types of data I might be getting from a JSON)

Khoi-Viet Le
  • 20
  • 2
  • 3
  • 1
    I'd suggest keeping track of the indentation with a third parameter, defaulting to zero. Also note lists can contain other lists or dicts, so should also be recursed on. – jonrsharpe Dec 21 '19 at 08:22
  • 1
    Your function is never inserting tabs at the start of a line, so it should be no surprise that the output has no lines that start with a tab. Secondly, tabs are single characters, they just fill up the space up to the *next* tab-stop (often a multiple of 4). So, the visual "size" of a tab depends wholly on the current offset. It can be 1, 2, 3 or 4. – trincot Dec 21 '19 at 08:23
  • This was answered before, refer to https://stackoverflow.com/a/42427233/7548672 –  Dec 21 '19 at 08:37
  • Thanks for the comments guys, it really helped me. Didn't know that tabs were just filling space to the next tab stop and didn't know about YAML. Though, I would still like to implement this myself rather than relying on a different module. – Khoi-Viet Le Dec 22 '19 at 02:54

1 Answers1

0
# fixed tab = 3 spaces
mytab = ' '*3

def string_info(json_info):
    string = ""
    if type(json_info) == dict:
        for key, value in json_info.items():
            other = string_info(value)
            other_tabbed = f'\n{mytab}'.join(other.split('\n'))
            string += f"\n{key}:{mytab}{other_tabbed}"
    elif type(json_info) == list:
        for index in range(0, len(json_info)):
            string += f"{json_info[index]} "
    else:
        string += f"{json_info}"
    return string


# test
import json

s = '''
{
"statistics": {
        "level": {
            "current": 100,
            "progress": 52
        },
        "pp": 5934,
        "pp_rank": 15496,
        "ranked_score": 15283968302,
        "hit_accuracy": 98.3355,
        "play_count": 76169,
        "play_time": 3855235,
        "total_score": 79160699555,
        "total_hits": 13126452,
        "maximum_combo": 2104,
        "replays_watched_by_others": 24,
        "is_ranked": true,
        "grade_counts": {
            "ss": 51,
            "ssh": 22,
            "s": 1202,
            "sh": 224,
            "a": 1272
        },
        "rank": {
            "global": 15496,
            "country": 2553
        }
    }
 }
'''

data = json.loads(s)
print(string_info(data))

Output:

statistics:   
   level:   
      current:   100
      progress:   52
   pp:   5934
   pp_rank:   15496
   ranked_score:   15283968302
   hit_accuracy:   98.3355
   play_count:   76169
   play_time:   3855235
   total_score:   79160699555
   total_hits:   13126452
   maximum_combo:   2104
   replays_watched_by_others:   24
   is_ranked:   True
   grade_counts:   
      ss:   51
      ssh:   22
      s:   1202
      sh:   224
      a:   1272
   rank:   
      global:   15496
      country:   2553
9mat
  • 1,194
  • 9
  • 13
  • Thanks, there's some pretty cool things I learned from python from this code. Also just to clarify, when you put two objects (in this case "key, value") in the for loop, what does it actually do? – Khoi-Viet Le Dec 22 '19 at 04:01
  • `json_info` is *kind of* a dictionary, which is *kind of* a list of key-value pairs. So `for k, v in json_info` will loops through the pairs, and assign the key to `k`, and the value to `v` – 9mat Dec 22 '19 at 04:06
  • Yes, if the list is a list of pairs (or tuples). try: `a = [(1,2),(3,4)]; for x,y in a: print(x,y);` You can even do: `a = [(1,(2,3)), (4,(5,6))]; for x,(y,z) in a: print(x,y,z);` – 9mat Dec 22 '19 at 04:09