Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sort data from JSON file
#1
Hi there! Have the following task:

***
data.json - file with data about ancient mathematicians.
1. Write function, which reads data from the file. Function parameter is the name of the file.
2. Write function which sorts data by surname in the field 'name' (for those who have it).
For example, in case of Rene Descartes surname is Descartes, for Pierre de Fermat it is Fermat etc.
If there is no surname, use name, for example Euclid.
***

It seems I've coped with the first part, but a bit confused by the second task. As the field name can contain only name, a name and surname, or a name, such word as "de" and surname. But we need to sort data by surname only. Not sure how to code it. Here's what I've done so far:

import json
def read_json(data):
    with open(data, "r", encoding='utf-8') as file:
        result = json.load(file)
    return result

def sort_by_surname(data):
    surname = 
# suppose here I should convert "name" field to a string, then use split to separate words, and make it use the latter word in case there are 2 or 3 words - sounds a bit complicated for me. Is there another way? Or is my version correct. Would be great if you could help me **huh** 
    sort_surname = sorted(data, key=abs(surname))
    print(sort_surname)
Pls help Huh Thanks in advance!
buran write Jan-04-2021, 08:47 PM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#2
Unless you have a "Surname" field, you're going to have to extract it from name, and that's annoying in edge cases. The final "word" seems to be quite reasonable as a first pass.

Not sure what you're doing with abs there in the key. That's not valid on strings.
Reply
#3
What is result?
result = json.load(file)
result is what you have to sort, and sorting requires you know about the things in result. What things do you have in result?

Sounds like you will have to write a function that knows how to get the surname from any object in result. Something like:
def surname(obj):
    if (value := obj.get('surname', None) is not None:
        return value

    if (value := obj.get('full name', None) is not None:
        return value.split(',')[0].strip()

    return ''

sorted_data = sorted(data, key=surname)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  encrypt data in json file help jacksfrustration 1 185 Mar-28-2024, 05:16 PM
Last Post: deanhystad
  parse json field from csv file lebossejames 4 722 Nov-14-2023, 11:34 PM
Last Post: snippsat
  Python Script to convert Json to CSV file chvsnarayana 8 2,487 Apr-26-2023, 10:31 PM
Last Post: DeaD_EyE
  Loop through json file and reset values [SOLVED] AlphaInc 2 2,076 Apr-06-2023, 11:15 AM
Last Post: AlphaInc
  Converting a json file to a dataframe with rows and columns eyavuz21 13 4,368 Jan-29-2023, 03:59 PM
Last Post: eyavuz21
  validate large json file with millions of records in batches herobpv 3 1,254 Dec-10-2022, 10:36 PM
Last Post: bowlofred
  Read nested data from JSON - Getting an error marlonbown 5 1,352 Nov-23-2022, 03:51 PM
Last Post: snippsat
  Reading Data from JSON tpolim008 2 1,073 Sep-27-2022, 06:34 PM
Last Post: Larz60+
  How to sort .csv file test log which item first fail and paint color SamLiu 24 4,800 Sep-03-2022, 07:32 AM
Last Post: Pedroski55
  Writing to json file ebolisa 1 993 Jul-17-2022, 04:51 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020