Expected Int - Got String

Afternoon all - Just received my Tidbyt and very impressed…

This could be a stupid question - i have got the Bitcoin example running, i have edited it to pull in a Json file from the UK Met Office, this returns a string. I cant seem to get it running - with an edit in the bitcoin example i get the error 'expected int, got string - current code below - feels close but no cigar. So the Q is - how do it pull in a string from Json and render the output?

load(“render.star”, “render”)
load(“http.star”, “http”)

met = http.get(“this goes to my metoffice data key”)

def main():
render = (met.json()[“RegionalFcst”][“FcstPeriods”][“Period”][“0”]["$"])

    return render.Root(
            child = render.Text(render))
)

Any help would be great,

Andy

It looks like you are assigning your JSON value to a variable name render which I think would conflict with the render module that is also imported as render.

Try something like this:

load("render.star", "render")
load("http.star", "http")

def main():
    met = http.get("this goes to my metoffice data key").json()
    amount = met["RegionalFcst"]["FcstPeriods"]["Period"]["0"]["$"]
    return render.Root(
            child = render.Text(content = amount)
    )

Thanks for the reply - that was quite a bad choice of variable name!

I have just tried it and i get the same error - so

Error: list index: got string, want int

So its happy if its an int but my json parse is a string…

Andy

Can you do print(amount) so we can see the raw JSON?

Starlark’s JSON module will do some type conversion, I wonder if the value is being converted on you. I’d recommend printing met and inspecting the response to check that.

If it is an int, you can cast an int to a string like this: str(amount)

Thanks for the replies - almost there - print works, so the Json is correct - next error is

Error: too many arguments for format string

From print:
A dry night for most. Long clear spells are likely overnight, although some patchy cloud is possible at times, especially near some Norfolk coasts with chance of a shower. Feeling cold with brisk winds continuing. Strong coastal winds Frost inland. Minimum Temperature -1C.

2022/01/04 20:09:31 error loading applet: error running script: Traceback (most recent call last):
bitcoin.star:15:40: in main
Error: too many arguments for format string

Oh I see - can you just try this for the last lines - contents should’ve been content:

return render.Root(
        child = render.Text(content = amount)
)

That worked :slight_smile: thanks so much for all the replies…

Now to fit the text on the display and scroll (marque I’m guessing)…

Andy

1 Like