Python Forum

Full Version: how to read json file
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3 4
(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.
(Feb-20-2020, 11:20 AM)jk91 Wrote: [ -> ]how should i make it according to my case also i just have eclispse software installed on my system
Use PyDev
PyDev Wrote:PyDev is a Python IDE for Eclipse

(Feb-20-2020, 11:20 AM)jk91 Wrote: [ -> ]i also tried to execute above code in colab but again it ended in error:-
You shall not use it like this,i do it in two parts read file then doing some test in interactive interpreter >>>.
The task will be hard without any basic knowledge of Python.

Here is what's in data.json test file.
Output:
{ "firstName": "Jane", "lastName": "Doe", "hobbies": [ "running", "sky diving", "singing" ], "age": 35, "children": [ { "firstName": "Alice", "age": 6 }, { "firstName": "Bob", "age": 8 } ] }
# test_read.py
import json
from pprint import pprint

with open("data.json", "r") as read_file:
    data = json.load(read_file)

pprint(data)
Output:
{'age': 35, 'children': [{'age': 6, 'firstName': 'Alice'}, {'age': 8, 'firstName': 'Bob'}], 'firstName': 'Jane', 'hobbies': ['running', 'sky diving', 'singing'], 'lastName': 'Doe'}
So now reading as a script test_read.py and run it,and i have to print the content.

Doing test in interactive interpreter >>>,i do not need print.
>>> data
{'age': 35,
 'children': [{'age': 6, 'firstName': 'Alice'}, {'age': 8, 'firstName': 'Bob'}],
 'firstName': 'Jane',
 'hobbies': ['running', 'sky diving', 'singing'],
 'lastName': 'Doe'}
>>> 
>>> data['hobbies']
['running', 'sky diving', 'singing']
>>> ', '.join(data['hobbies'])
'running, sky diving, singing'

# Can also use print here
>>> print(', '.join(data['hobbies']))
running, sky diving, singing
>>> 
(Feb-20-2020, 12:07 PM)snippsat Wrote: [ -> ]
(Feb-20-2020, 11:20 AM)jk91 Wrote: [ -> ]how should i make it according to my case also i just have eclispse software installed on my system
Use PyDev
PyDev Wrote:PyDev is a Python IDE for Eclipse

(Feb-20-2020, 11:20 AM)jk91 Wrote: [ -> ]i also tried to execute above code in colab but again it ended in error:-
You shall not use it like this,i do it in two parts read file then doing some test in interactive interpreter >>>.
The task will be hard without any basic knowledge of Python.

Here is what's in data.json test file.
Output:
{ "firstName": "Jane", "lastName": "Doe", "hobbies": [ "running", "sky diving", "singing" ], "age": 35, "children": [ { "firstName": "Alice", "age": 6 }, { "firstName": "Bob", "age": 8 } ] }
# test_read.py
import json
from pprint import pprint

with open("data.json", "r") as read_file:
    data = json.load(read_file)

pprint(data)
Output:
{'age': 35, 'children': [{'age': 6, 'firstName': 'Alice'}, {'age': 8, 'firstName': 'Bob'}], 'firstName': 'Jane', 'hobbies': ['running', 'sky diving', 'singing'], 'lastName': 'Doe'}
So now reading as a script test_read.py and run it,and i have to print the content.

Doing test in interactive interpreter >>>,i do not need print.
>>> data
{'age': 35,
 'children': [{'age': 6, 'firstName': 'Alice'}, {'age': 8, 'firstName': 'Bob'}],
 'firstName': 'Jane',
 'hobbies': ['running', 'sky diving', 'singing'],
 'lastName': 'Doe'}
>>> 
>>> data['hobbies']
['running', 'sky diving', 'singing']
>>> ', '.join(data['hobbies'])
'running, sky diving, singing'

# Can also use print here
>>> print(', '.join(data['hobbies']))
running, sky diving, singing
>>> 

if i just download pydev ide will it not require to install python separately also i am on windows 7 32 bit and tried to install sp1 for it but it's getting failed so alternatively someone advised me to use colab.
so with colab can't i just test above all?

(Feb-20-2020, 02:06 PM)jk91 Wrote: [ -> ]
(Feb-20-2020, 12:07 PM)snippsat Wrote: [ -> ]Use PyDev

You shall not use it like this,i do it in two parts read file then doing some test in interactive interpreter >>>.
The task will be hard without any basic knowledge of Python.

Here is what's in data.json test file.
Output:
{ "firstName": "Jane", "lastName": "Doe", "hobbies": [ "running", "sky diving", "singing" ], "age": 35, "children": [ { "firstName": "Alice", "age": 6 }, { "firstName": "Bob", "age": 8 } ] }
# test_read.py
import json
from pprint import pprint

with open("data.json", "r") as read_file:
    data = json.load(read_file)

pprint(data)
Output:
{'age': 35, 'children': [{'age': 6, 'firstName': 'Alice'}, {'age': 8, 'firstName': 'Bob'}], 'firstName': 'Jane', 'hobbies': ['running', 'sky diving', 'singing'], 'lastName': 'Doe'}
So now reading as a script test_read.py and run it,and i have to print the content.

Doing test in interactive interpreter >>>,i do not need print.
>>> data
{'age': 35,
 'children': [{'age': 6, 'firstName': 'Alice'}, {'age': 8, 'firstName': 'Bob'}],
 'firstName': 'Jane',
 'hobbies': ['running', 'sky diving', 'singing'],
 'lastName': 'Doe'}
>>> 
>>> data['hobbies']
['running', 'sky diving', 'singing']
>>> ', '.join(data['hobbies'])
'running, sky diving, singing'

# Can also use print here
>>> print(', '.join(data['hobbies']))
running, sky diving, singing
>>> 


if i just download pydev ide will it not require to install python separately also i am on windows 7 32 bit and tried to install sp1 for it but it's getting failed so alternatively someone advised me to use colab.
so with colab can't i just test above all?

also where do we save that data.json test file?also what is the type we give it while saving it and what should be the location of aving it and can we save it it in plain note pad text file?
(Feb-20-2020, 02:10 PM)jk91 Wrote: [ -> ]so with colab can't i just test above all?
Yes you have to upload the data.json file first on right side menu.
This work in Notebook format .ipynb and can run files and do interactive testing.
Here running the file and doing some interactive testing push + Code for new cell.
[Image: U5vLRc.png]
(Feb-20-2020, 02:38 PM)snippsat Wrote: [ -> ]
(Feb-20-2020, 02:10 PM)jk91 Wrote: [ -> ]so with colab can't i just test above all?
Yes you have to upload the data.json file first on right side menu.
This work in Notebook format .ipynb and can run files and do interactive testing.
Here running the file and doing some interactive testing push + Code for new cell.
[Image: U5vLRc.png]

When trying to upload this data.json notepad file which i have saved on my desktop I am getting attached error.

The document does not appear to be a correctly formatted notebook. Version information is missing in the JSON.
CustomError: The document does not appear to be a correctly formatted notebook. Version information is missing in the JSON.
at new Fx (https://colab.research.google.com/v2/ext...580:866:76)
at KF (https://colab.research.google.com/v2/ext...0:1216:528)
at https://colab.research.google.com/v2/ext...0:1601:497
at e.onFulfilled (https://colab.research.google.com/v2/ext...80:372:216)
at Zl (https://colab.research.google.com/v2/ext...80:376:306)
at Vl (https://colab.research.google.com/v2/ext...80:376:164)
at Dl.executeCallbacks_ (https://colab.research.google.com/v2/ext...220-085600-
You shall not do File --> Upload Notebook.
Is menu on left side files.
[Image: ZZHMwc.png]
yes, i did same tried to upload from same menu navigation as advised here but getting the mentioned error not sure why.

(Feb-23-2020, 11:03 AM)jk91 Wrote: [ -> ]yes, i did same tried to upload from same menu navigation as advised here but getting the mentioned error not sure why.

attached is my data.json file.
Do you not see that file has ipynd in file extension.
The file shall only be data.json.
Download it here and try.
i tried to download from your link but after downloading it,i can not open it and my computer say's look for appropriate program from internet to open such files also i tried to save my data.json file uploaded in my last post as .ipynd in file extension but some how that notepad file gets saved in .txt format only not sure what is wrong here.
how was thisfilewith extension .ipynd generated?
Pages: 1 2 3 4