1

I am trying to do something similar to the below code but instead of printing like last line in the code, I am looking for output to display as list of dicts.

import json
studentsList = []
print("Started Reading JSON file which contains multiple JSON document")
with open('students.txt') as f:
    for jsonObj in f:
    studentDict = json.loads(jsonObj)
    studentsList.append(studentDict)
print("Printing each JSON Decoded Object")
for student in studentsList:
    print(studentList) # Output is not looping , I get the following [{'id': 
    1, 'first_name': 'name1'}] but repeated 10 times in row instead it needs 
    to increment id until 10 with 1-10 id). 

Thanks

1
  • 1
    Can you give an example of input file for testing? Commented May 9, 2020 at 8:11

2 Answers 2

2

If you simply want to print the list of dicts, why don't you just do print(studentsList)

It would look like this:

import json
studentsList = []
print("Started Reading JSON file which contains multiple JSON document")
with open('students.txt') as f:
    for jsonObj in f:
      studentDict = json.loads(jsonObj)
      studentsList.append(studentDict)
print("Printing the list of JSON Decoded Objects")
print(studentList)

OUTPUT: Without Loop

If you want your loop to work:

import json
studentsList = []
print("Started Reading JSON file which contains multiple JSON document")
with open('students.txt') as f:
    for jsonObj in f:
      studentDict = json.loads(jsonObj)
      studentsList.append(studentDict)
print("Printing the list of JSON Decoded Objects")
for student in studentsList:
    print(student)

OUTPUT: With Loop

Sign up to request clarification or add additional context in comments.

10 Comments

Lets say I have the dictionary a following. When I print(studentList) I get 4 rows with first row repeated 4 times which is not what i am looking for. I want 4 rows with below data as list dicts. dict = {"id": 1, "name": "Ault", "class": 8, "email": "[email protected]"} {"id": 2, "name": "john", "class": 8, "email": "[email protected]"} {"id": 3, "name": "josh", "class": 8, "email": "[email protected]"} {"id": 4, "name": "emma", "class": 8, "email": "[email protected]"}
If you only see your first row in print(studentList), then you have a problem when creating the objects, before printing them. Give a sample of input file, please.
I took the example from the previous comment (which is the same) and I've updated my answer.
That worked. However, I used pprint module to align the output. How can I sort and get the data similar to the input file. Looks like it is sorting alphabetically. Please advise. [{'class': 8, 'email': '[email protected]', 'id': 1, 'name': 'Ault'}, {'class': 8, 'email': '[email protected]', 'id': 2, 'name': 'john'}, {'class': 8, 'email': '[email protected]', 'id': 3, 'name': 'josh'}, {'class': 8, 'email': '[email protected]', 'id': 4, 'name': 'emma'}]
studentsList = sorted(studentsList, key=lambda k: k['name'].lower()) will sort the list according to the name in lowercase. You need lowercase because otherwise capitalised names will always be first. This will not change your data, just the sorting. Insert this before beginning printing.
|
0

This worked for me to avoid sorting alphabetically the list. pprint messes the sort so had to use below to make it work.

pprint(students_list, sort_dicts=False )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.