2

I'm trying to get data in JSON file, but before I print out, I want to sort the money. This is my code

with open("cash.json", "r", encoding="utf-8") as f:
    data = json.load(f)
    for c in data['NotAdmin']:
        a_new_list = sorted(c["money"])
        print(a_new_list)

This is what is inside my cash.json file

{
    "NotAdmin": [
        {
            "money": 200.0,
            "name": "Sam",
            "password": "1234"
        }, {
            "money": 150.0,
            "name": "Jasmin",
            "password": "1573"
        }, {
            "money": 100.0,
            "name": "Ali",
            "password": "1856"
        }
    ],
    "admin": [
        {
            "name": "Adam",
            "password": "6767"
        }, {
            "name": "Eva",
            "password": "2222"
        }
    ]
}

Im keep getting this error TypeError: 'float' object is not iterable

vvvvv
  • 25,404
  • 19
  • 49
  • 81
sadiq ali
  • 67
  • 1
  • 8

5 Answers5

2

The reason that you are getting the TypeError is that you were iterating through the dictionaries in the NotAdmin list and trying to apply to the sorted functions to the money attributes. This will obviously fail as you are trying to "sort" a float(?).

So, the way the sorted (or sort function which modifies the iterable in place) works is to take an iterable and apply an "evaluation" function to each element, in order to sort them. This function can either be passed in under the key parameter, or if not passed in, Python will automatically decide how to sort the elements.

To apply this to our code, we want to take the data["NotAdmin"] list and apply the .sort method with a key to select the money attribute of each of the dictionaries. We can do this with an (anonymous) lambda function.

So, here is the full code:

with open("cash.json", "r", encoding="utf-8") as f:
    data = json.load(f)
    data["NotAdmin"].sort(key = lambda d: d["money"])
    print(data)

output:

{'admin': [{'password': '6767', 'name': 'Adam'}, {'password': '2222', 'name': 'Eva'}], 'NotAdmin': [{'password': '1856', 'money': 100.0, 'name': 'Ali'}, {'password': '1573', 'money': 150.0, 'name': 'Jasmin'}, {'password': '1234', 'money': 200.0, 'name': 'Sam'}]}

And also, if you want to have the elements in descending order, then we can take a look at the documentation and see that we can set the reverse flag to True.

Which would just change the line to:

...
    ...
    data["NotAdmin"].sort(key = lambda d: d["money"], reverse = True)
    ...
Joe Iddon
  • 20,101
  • 7
  • 33
  • 54
2

What you want to do is:

with open("cash.json", "r", encoding="utf-8") as f:
    data = json.load(f)
    a_new_list = sorted(data['NotAdmin'], key=lambda k: k['money'])
    print(a_new_list)

By doing sorted(c["money"]), you want to sort one float, the money of the dictionary c, which is not possible, nor what you want.

vvvvv
  • 25,404
  • 19
  • 49
  • 81
1

You can try this:

with open("cash.json", "r", encoding="utf-8") as f:
   data = json.load(f)
   new_data = {a:sorted(b, key=lambda x:x['money']) if all('money' in c for c in b) else b for a, b in data.items()}

Output:

{'NotAdmin': [{'money': 100.0, 'password': '1856', 'name': 'Ali'}, {'money': 150.0, 'password': '1573', 'name': 'Jasmin'}, {'money': 200.0, 'password': '1234', 'name': 'Sam'}], 'admin': [{'password': '6767', 'name': 'Adam'}, {'password': '2222', 'name': 'Eva'}]}
Ajax1234
  • 69,937
  • 8
  • 61
  • 102
0

The error you got is due to its definition

sorted??
Docstring: sorted(iterable, cmp=None, key=None, reverse=False) --> new sorted list
Type:      builtin_function_or_method

To tackle your problem, you need to sort array object using their attributes, example

with open("cash.json", "r", encoding="utf-8") as f:
    data = json.load(f)
    for c in data['NotAdmin']:
        a_new_list = sorted(c, key=lambda x: x['money'], reverse=True)
        print(a_new_list)

see more: How to sort a list of objects based on an attribute of the objects?

Bryan
  • 1,477
  • 1
  • 21
  • 38
  • this is close to what I'm looking for, but there is an error with this TypeError: string indices must be integers. – sadiq ali Jan 01 '18 at 15:54
0

This is one line solution , You can try

Approach is , first check if key is 'NotAdmin' and then sorted key is 'money'

import json
print([sorted(value, key=lambda x: x['money']) for key,value in json.loads(open('data.json','r').read()).items() if key=='NotAdmin'])

output:

[[{'name': 'Ali', 'money': 100.0, 'password': '1856'}, {'name': 'Jasmin', 'money': 150.0, 'password': '1573'}, {'name': 'Sam', 'money': 200.0, 'password': '1234'}]]