Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to read json file
#11
(Feb-20-2020, 10:03 AM)snippsat Wrote: Look like you have been given this a task as homework.
With some Java code as a start and that you should write it Python.
So is this homework?

Just to give some hint on a start.
jk91 Wrote:Step 1: You have to read the file, If any Error, please correct.
import json

with open("data.json", "r") as read_file:
    data = json.load(read_file)
This read the json file,look into try:except for catching error.

What the json file return is Python dictionary,here is it normal to have a mix of dict and list as your data show.
Can use direct access or a get method as the Java code has.
>>> data
{'age': 35,
 'children': [{'age': 6, 'firstName': 'Alice'}, {'age': 8, 'firstName': 'Bob'}],
 'firstName': 'Jane',
 'hobbies': ['running', 'sky diving', 'singing'],
 'lastName': 'Doe'}

>>> # Direct access
>>> data['children'][0]['firstName']
'Alice'
>>> 
>>> # Using get here can also do error check
>>> data['children'][0].get('firstName', 'Not in Record')
'Alice'
>>> data['children'][0].get('firstName9', 'Not in Record')
'Not in Record' 

it's a problem which i am trying to solve:-
how should i make it according to my case also i just have eclispse software installed on my system so with that will i be able to test this code and test it in python or in java if yes then how please guide.

Thanks

(Feb-20-2020, 10:03 AM)snippsat Wrote: Look like you have been given this a task as homework.
With some Java code as a start and that you should write it Python.
So is this homework?

Just to give some hint on a start.
jk91 Wrote:Step 1: You have to read the file, If any Error, please correct.
import json

with open("data.json", "r") as read_file:
    data = json.load(read_file)
This read the json file,look into try:except for catching error.

What the json file return is Python dictionary,here is it normal to have a mix of dict and list as your data show.
Can use direct access or a get method as the Java code has.
>>> data
{'age': 35,
 'children': [{'age': 6, 'firstName': 'Alice'}, {'age': 8, 'firstName': 'Bob'}],
 'firstName': 'Jane',
 'hobbies': ['running', 'sky diving', 'singing'],
 'lastName': 'Doe'}

>>> # Direct access
>>> data['children'][0]['firstName']
'Alice'
>>> 
>>> # Using get here can also do error check
>>> data['children'][0].get('firstName', 'Not in Record')
'Alice'
>>> data['children'][0].get('firstName9', 'Not in Record')
'Not in Record' 
i also tried to execute above code in colab but again it ended in error:-

(Feb-20-2020, 11:20 AM)jk91 Wrote:
(Feb-20-2020, 10:03 AM)snippsat Wrote: Look like you have been given this a task as homework.
With some Java code as a start and that you should write it Python.
So is this homework?

Just to give some hint on a start.
import json

with open("data.json", "r") as read_file:
    data = json.load(read_file)
This read the json file,look into try:except for catching error.

What the json file return is Python dictionary,here is it normal to have a mix of dict and list as your data show.
Can use direct access or a get method as the Java code has.
>>> data
{'age': 35,
 'children': [{'age': 6, 'firstName': 'Alice'}, {'age': 8, 'firstName': 'Bob'}],
 'firstName': 'Jane',
 'hobbies': ['running', 'sky diving', 'singing'],
 'lastName': 'Doe'}

>>> # Direct access
>>> data['children'][0]['firstName']
'Alice'
>>> 
>>> # Using get here can also do error check
>>> data['children'][0].get('firstName', 'Not in Record')
'Alice'
>>> data['children'][0].get('firstName9', 'Not in Record')
'Not in Record' 

it's a problem which i am trying to solve:-
how should i make it according to my case also i just have eclispse software installed on my system so with that will i be able to test this code and test it in python or in java if yes then how please guide.

Thanks

(Feb-20-2020, 10:03 AM)snippsat Wrote: Look like you have been given this a task as homework.
With some Java code as a start and that you should write it Python.
So is this homework?

Just to give some hint on a start.
import json

with open("data.json", "r") as read_file:
    data = json.load(read_file)
This read the json file,look into try:except for catching error.

What the json file return is Python dictionary,here is it normal to have a mix of dict and list as your data show.
Can use direct access or a get method as the Java code has.
>>> data
{'age': 35,
 'children': [{'age': 6, 'firstName': 'Alice'}, {'age': 8, 'firstName': 'Bob'}],
 'firstName': 'Jane',
 'hobbies': ['running', 'sky diving', 'singing'],
 'lastName': 'Doe'}

>>> # Direct access
>>> data['children'][0]['firstName']
'Alice'
>>> 
>>> # Using get here can also do error check
>>> data['children'][0].get('firstName', 'Not in Record')
'Alice'
>>> data['children'][0].get('firstName9', 'Not in Record')
'Not in Record' 
i also tried to execute above code in colab but again it ended in error:-

i need to use colab as unable to install python on my windows 7 32 bit operating system as it requires sp1 but it could not be installed successfully so had to use colab for this problem. but unfortunately it is also not working and when clicking on left side traingle button to run this program it is ending up in error.

Thanks.

Attached Files

Thumbnail(s)
   
Reply


Messages In This Thread
how to read json file - by jk91 - Feb-18-2020, 11:48 AM
RE: how to read json file - by Larz60+ - Feb-18-2020, 06:02 PM
RE: how to read json file - by ndc85430 - Feb-18-2020, 06:10 PM
RE: how to read json file - by jk91 - Feb-19-2020, 05:44 AM
RE: how to read json file - by ndc85430 - Feb-19-2020, 06:41 AM
RE: how to read json file - by jk91 - Feb-19-2020, 07:28 AM
RE: how to read json file - by jk91 - Feb-20-2020, 06:17 AM
RE: how to read json file - by jk91 - Feb-20-2020, 09:18 AM
RE: how to read json file - by buran - Feb-20-2020, 09:40 AM
RE: how to read json file - by snippsat - Feb-20-2020, 10:03 AM
RE: how to read json file - by jk91 - Feb-20-2020, 11:20 AM
RE: how to read json file - by snippsat - Feb-20-2020, 12:07 PM
RE: how to read json file - by jk91 - Feb-20-2020, 02:10 PM
RE: how to read json file - by snippsat - Feb-20-2020, 02:38 PM
RE: how to read json file - by jk91 - Feb-23-2020, 06:37 AM
RE: how to read json file - by snippsat - Feb-23-2020, 10:49 AM
RE: how to read json file - by jk91 - Feb-23-2020, 11:03 AM
RE: how to read json file - by snippsat - Feb-23-2020, 12:06 PM
RE: how to read json file - by jk91 - Feb-23-2020, 01:17 PM
RE: how to read json file - by jk91 - Feb-23-2020, 02:24 PM
RE: how to read json file - by snippsat - Feb-23-2020, 03:30 PM
RE: how to read json file - by jk91 - Feb-23-2020, 04:26 PM
RE: how to read json file - by snippsat - Feb-23-2020, 04:50 PM
RE: how to read json file - by jk91 - Feb-24-2020, 11:56 AM
RE: how to read json file - by jk91 - Feb-24-2020, 01:15 PM
RE: how to read json file - by buran - Feb-24-2020, 01:28 PM
RE: how to read json file - by jk91 - Feb-24-2020, 01:38 PM
RE: how to read json file - by snippsat - Feb-24-2020, 01:45 PM
RE: how to read json file - by jk91 - Feb-24-2020, 06:22 PM
RE: how to read json file - by snippsat - Feb-24-2020, 07:45 PM
RE: how to read json file - by jk91 - Feb-25-2020, 04:11 PM
RE: how to read json file - by t4keheart - Feb-25-2020, 02:08 PM
RE: how to read json file - by t4keheart - Feb-25-2020, 06:24 PM
RE: how to read json file - by jk91 - Feb-26-2020, 08:10 AM
RE: how to read json file - by snippsat - Feb-26-2020, 05:07 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Connecting to Remote Server to read contents of a file ChaitanyaSharma 1 267 May-03-2024, 07:23 AM
Last Post: Pedroski55
  encrypt data in json file help jacksfrustration 1 300 Mar-28-2024, 05:16 PM
Last Post: deanhystad
  Recommended way to read/create PDF file? Winfried 3 2,966 Nov-26-2023, 07:51 AM
Last Post: Pedroski55
  parse json field from csv file lebossejames 4 807 Nov-14-2023, 11:34 PM
Last Post: snippsat
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 1,595 Nov-09-2023, 10:56 AM
Last Post: mg24
  read file txt on my pc to telegram bot api Tupa 0 1,172 Jul-06-2023, 01:52 AM
Last Post: Tupa
  parse/read from file seperated by dots giovanne 5 1,164 Jun-26-2023, 12:26 PM
Last Post: DeaD_EyE
  Formatting a date time string read from a csv file DosAtPython 5 1,407 Jun-19-2023, 02:12 PM
Last Post: DosAtPython
  How do I read and write a binary file in Python? blackears 6 7,185 Jun-06-2023, 06:37 PM
Last Post: rajeshgk
  Python Script to convert Json to CSV file chvsnarayana 8 2,617 Apr-26-2023, 10:31 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

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