Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Reading a json file
#1
Hi,
I've a file with the following data:
[{"hour": "09", "minutes": "00"}, {"ext_ip": "82.334.188.22"}, {"status": "1"}]
A test code reads the file content and prints out the data type:
import time, json

datajs = '/home/pi/data/data.json'

set_hr = 8 #opening hour
set_min = 0 #opening minute
ext_ip = ''
status = 1 #blinds activated

def read_json():
    global set_hr, set_min, ext_ip, status
    f = open(datajs, 'r')
    data = json.load(f)
    set_hr = int(data[0]['hour'])
    set_min = int(data[0]['minutes'])
    ext_ip = data[1]['ext_ip']
    status = data[2]['status']
    f.close()

read_json()

print('hr type: {} {}'.format(type(set_hr), set_hr))
print('min type: {} {}'.format(type(set_min), set_min))
print('ip type: {} {}'.format(type(ext_ip), str(ext_ip)))
print('status type: {} {}'.format(type(status), status))
and these are the results:
hr type: <class 'int'> 9
min type: <class 'int'> 0
ip type: <class 'str'> 82.213.217.163
status type: <class 'str'> 1
Why the status data is shown as a string when it's an int?
TIA
Reply
#2
(Mar-15-2020, 09:10 AM)ebolisa Wrote: Why the status data is shown as a string when it's an int?
from what you show it's a string - it is enclosed in quotes and you don't cast it to int like you do with hour and minutes values

it's interesting why it's a list of dicts, and not single dict (I speak in terms of python objects, not json array/object terms)
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
Bonkself I new it was some stupid! Thanks.

(Mar-15-2020, 09:15 AM)buran Wrote: it's interesting why it's a list of dicts, and not single dict (I speak in terms of python objects, not json array/object terms)

It's a file generated by a php code.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  encrypt data in json file help jacksfrustration 1 180 Mar-28-2024, 05:16 PM
Last Post: deanhystad
Sad problems with reading csv file. MassiJames 3 608 Nov-16-2023, 03:41 PM
Last Post: snippsat
  parse json field from csv file lebossejames 4 720 Nov-14-2023, 11:34 PM
Last Post: snippsat
  Reading a file name fron a folder on my desktop Fiona 4 889 Aug-23-2023, 11:11 AM
Last Post: Axel_Erfurt
  TypeRoor reading json GreenLynx 3 844 May-16-2023, 01:47 PM
Last Post: buran
  Python Script to convert Json to CSV file chvsnarayana 8 2,486 Apr-26-2023, 10:31 PM
Last Post: DeaD_EyE
  Loop through json file and reset values [SOLVED] AlphaInc 2 2,071 Apr-06-2023, 11:15 AM
Last Post: AlphaInc
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,085 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Converting a json file to a dataframe with rows and columns eyavuz21 13 4,364 Jan-29-2023, 03:59 PM
Last Post: eyavuz21
  Reading a file JonWayn 3 1,089 Dec-30-2022, 10:18 AM
Last Post: ibreeden

Forum Jump:

User Panel Messages

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