PyPortal(json_path= help

CircuitPython on hardware including Adafruit's boards, and CircuitPython libraries using Blinka on host computers.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
teltech84
 
Posts: 20
Joined: Sun Jan 23, 2022 10:20 am

PyPortal(json_path= help

Post by teltech84 »

I have a json file that looks like this.

Code: Select all

{
    "0": {
        "": "0",
        "Unnamed: 0": "0",
        "entryDate": "2021-12-17",
        "entryContent": "I went to mexico and had fun.",
        "id": "0"
    }
}
However when I call it using the following code it doesn't find the json path. I'm not too familiar with json addressing.

Code: Select all

import time
import board
from adafruit_pyportal import PyPortal
import json



i = 0
 
#Set up where we'll be fetching data from
DATA_SOURCE = "http://192.168.1.67/presently-backup.json"
QUOTE_LOCATION = [i , 'entryContent']
AUTHOR_LOCATION = [i , 'entryDate']

#the current working directory (where this file is)
cwd = ("/"+__file__).rsplit('/', 1)[0]
var = (QUOTE_LOCATION, AUTHOR_LOCATION)
pyportal = PyPortal(url=DATA_SOURCE,
                    json_path=(QUOTE_LOCATION, AUTHOR_LOCATION),
                    #status_neopixel=board.NEOPIXEL,
                    default_bg=cwd+"/quote_background.bmp",
                    text_font=cwd+"/fonts/Arial-ItalicMT-17.bdf",
                    text_position=((20, 120),  # quote location
                                (5, 210)), # author location
                    text_color=(0xFFFFFF,  # quote text color
                                0x8080FF), # author text color
                    text_wrap=(35, # characters to wrap for quote
                            0), # no wrap for author
                    text_maxlen=(180, 30), # max text size for quote & author
                )
pyportal.preload_font()


def main(i):
    
    pyportal.json_path=([ i,
                        'entryContent'],
                        [ i,
                        'entryDate'],
                        )
    # speed up projects with lots of text by preloading the font!
    value = pyportal.fetch()
    print("Response is", value)


while True:
    try:
        main(i)
        i = i + 1
        time.sleep(5)
    except (ValueError, RuntimeError) as e:
        print("Some error occured, retrying! -", e)
    time.sleep(5)
It works when I use a json file like this like in the example but it doesn't work in the new json.

Code: Select all

[{"text":"testtext1","author":"author1"},
{"text":"testtext2","author":"author2"}]

User avatar
dastels
 
Posts: 15831
Joined: Tue Oct 20, 2015 3:22 pm

Re: PyPortal(json_path= help

Post by dastels »

Because the outer structure is a dictionary in your "real" data, not an array. And it's keyed with strings representing numbers.

So instead on using

Code: Select all

    pyportal.json_path=([ i,
                        'entryContent'],
                        [ i,
                        'entryDate'],
                        )
Use

Code: Select all

    pyportal.json_path=([ str(i),
                        'entryContent'],
                        [ str(i),
                        'entryDate'],
                        )
That's assuming that that top level key is sequential "0", "1", "2", ...

Dave

User avatar
teltech84
 
Posts: 20
Joined: Sun Jan 23, 2022 10:20 am

Re: PyPortal(json_path= help

Post by teltech84 »

Never would have guessed that.

Thanks again! works like a charm.

Final code:

Code: Select all

import time
import board
from adafruit_pyportal import PyPortal
import json



i = 0
 
#Set up where we'll be fetching data from
DATA_SOURCE = "http://192.168.1.67/presently-backup.json"
QUOTE_LOCATION = [i, "entryContent"]
AUTHOR_LOCATION = [i, "entryDate"]

#the current working directory (where this file is)
cwd = ("/"+__file__).rsplit('/', 1)[0]
var = (QUOTE_LOCATION, AUTHOR_LOCATION)
pyportal = PyPortal(url=DATA_SOURCE,
                    json_path=(QUOTE_LOCATION, AUTHOR_LOCATION),
                    #status_neopixel=board.NEOPIXEL,
                    default_bg=cwd+"/quote_background.bmp",
                    text_font=cwd+"/fonts/PTSans-BoldItalic.bdf",
                    text_position=((20, 120),  # quote location
                                (5, 210)), # author location
                    text_color=(0xBF40BF,  # quote text color
                                0x8080FF), # author text color
                    text_wrap=(35, # characters to wrap for quote
                            0), # no wrap for author
                    text_maxlen=(180, 30), # max text size for quote & author
                )
pyportal.preload_font()



def main(i):
    
    pyportal.json_path=([str(i),
                        'entryContent'],
                        [str(i),
                        'entryDate'],
                        )
    # speed up projects with lots of text by preloading the font!
    value = pyportal.fetch()
    print("Response is", value)


while True:
    try:
        main(i)
        i = i + 1
        time.sleep(5)
    except (ValueError, RuntimeError) as e:
        print("Some error occured, retrying! -", e)
    time.sleep(5)

User avatar
dastels
 
Posts: 15831
Joined: Tue Oct 20, 2015 3:22 pm

Re: PyPortal(json_path= help

Post by dastels »

Good.

You need to pay attention to the JSON brackets. {} contain a dictionary while [] contain an array. There's also the key : value structure of the dictionary.

Dave

Locked
Please be positive and constructive with your questions and comments.

Return to “Adafruit CircuitPython”