Python Forum
first time pyton user - help with json
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
first time pyton user - help with json
#1
Hello ,
I want to read a json file and get 2 objects form the file (username and IP )
the config.json is :
Output:
{ "IP":"172.16.3.1", "username" : "admin", "password" : "Root", "Remote":"172.16.3.254", "internalNumber":31", "comId":2, }
how do I do this?
this is what I did to read the all file :
import json

with open('/home/pi/Documents/config.json') as json_file:
    data = json.load(json_file)
    print(data)
I have also try to use:
j = json.loads('/home/pi/Documents/config.json')
    print (j['IP'])
but I get error

so what am I missing?


Thanks,
Reply
#2
if that's the actual json file - it's not a valid one
look at "internalNumber":31"
it should be "internalNumber":31 or "internalNumber":"31"

and your first snippet would work.

the second one would not work - you need to read the entire file as string and pass it this string to json.loads(), not the filepath
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
The json file is missing " and has an extra , on the end
Output:
{ "IP":"172.16.3.1", "username" : "admin", "password" : "Root", "Remote": "172.16.3.254", "internalNumber": "31", "comId": 2 }
Works
Reply
#4
(May-06-2019, 11:06 AM)Yoriz Wrote: and has an extra , on the end
yep, I missed that
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
OK - saw and fix the problem
Thanks !
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [split] Help- converting file with pyton script eltomassito 6 3,243 Jul-02-2021, 05:29 PM
Last Post: snippsat
  Help- converting file with pyton script grinleon 3 2,481 Sep-23-2020, 11:48 AM
Last Post: grinleon
  How to time a user's input Tbot1000 2 2,530 Aug-31-2020, 09:39 PM
Last Post: Larz60+
  from Json Time Serie file to python dictionnary Reims 1 2,022 Sep-11-2019, 08:17 AM
Last Post: DeaD_EyE
  Starter programing using Pyton for Madagascar software. Rosendo 1 2,728 Jun-16-2018, 05:46 AM
Last Post: buran

Forum Jump:

User Panel Messages

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