←  APIs and Utilities

ComputerCraft | Programmable Computers for Minecraft

»

JSON API v2.0.1 for ComputerCraft

jonny190's Photo jonny190 09 Jul 2017

I'm trying to use obj.media_series_title but its outputting nothing, is there a way to use underscores?
Quote

Lignum's Photo Lignum 09 Jul 2017

View Postjonny190, on 09 July 2017 - 11:03 PM, said:

I'm trying to use obj.media_series_title but its outputting nothing, is there a way to use underscores?

That should work, so the problem is most likely somewhere else. Post the rest of the relevant code, please.

Either way, an idiotproof method that works with any string is to use [] to index obj:
obj["media_series_title"]
Quote

jonny190's Photo jonny190 10 Jul 2017

Dont Laugh too hard at my lack on coding ability
while true do
monitor = peripheral.wrap("back")
os.loadAPI("json")
str = http.get("https://home.daveys.xyz/api/states/media_player.firetv").readAll()
obj = json.decode(str)
monitor.clear()
monitor.setCursorPos(1,2)
monitor.write("Kodi is = ")
monitor.write(obj.state)
monitor.setCursorPos(1,4)
monitor.write("Kodi is playing", obj.media_series_title)
os.sleep( 3 )
term.clear()
monitor.clear()
monitor.setCursorPos(1,2)
sleep(0.05)
end

Edited by jonny190, 10 July 2017 - 06:39 PM.
Quote

Lignum's Photo Lignum 10 Jul 2017

View Postjonny190, on 10 July 2017 - 05:28 PM, said:

Dont Laugh too hard at my lack on coding ability

Oh, don't worry, there's significantly worse to be found around here.

That code looks fine to me, though. Does the monitor.write(obj.state) bit work? This is odd because if media_series_title wasn't indexed properly, it would write "nil" on the screen, not nothing, unless it's out of bounds for some reason. Are you sure the server is returning correct json? You might want to put print(str) after you http.get it to ensure that it's correct.
Quote

jonny190's Photo jonny190 10 Jul 2017

good to know,

The Webserver outputs:
"attributes": {"entity_picture": "/api/media_player_proxy/media_player.firetv?token=17f0d625441c059b681a532151a980af4dec61f14ccccaca413a9ec59646168d&cache=c61e3", "friendly_name": "FireTV", "is_volume_muted": false, "media_album_name": "", "media_content_id": {"unknown": ""}, "media_content_type": "tvshow", "media_duration": 1270, "media_episode": 15, "media_season": 1, "media_series_title": "American Dad!", "media_title": "American Dad S01E15", "supported_features": 55103, "volume_level": 1.0}, "entity_id": "media_player.firetv", "last_changed": "2017-07-10T18:42:20.419488+00:00", "last_updated": "2017-07-10T18:42:20.419488+00:00", "state": "playing"}

and the Monitor shows:
Posted Image
Edited by jonny190, 10 July 2017 - 07:13 PM.
Quote

jonny190's Photo jonny190 10 Jul 2017

Sorted it, looks like i forgot about the initial attributes section, changed it to obj.attributes.media_series_title and everything works no problem :D

Can this API be used to send commands in JSON?
Quote

Lignum's Photo Lignum 10 Jul 2017

View Postjonny190, on 10 July 2017 - 07:13 PM, said:

Can this API be used to send commands in JSON?

I'm not sure about what exactly you mean, but you can use json.encode to turn any table into a JSON-encoded string. Though, if you mean a literal Minecraft server command, you should use textutils.serialiseJSON, since the format isn't actually entirely JSON, it's slightly different.
Quote

Bomb Bloke's Photo Bomb Bloke 11 Jul 2017

The alternate format is referred to as a data tag.
Quote

jonny190's Photo jonny190 11 Jul 2017

View PostLignum, on 10 July 2017 - 10:04 PM, said:

View Postjonny190, on 10 July 2017 - 07:13 PM, said:

Can this API be used to send commands in JSON?

I'm not sure about what exactly you mean, but you can use json.encode to turn any table into a JSON-encoded string. Though, if you mean a literal Minecraft server command, you should use textutils.serialiseJSON, since the format isn't actually entirely JSON, it's slightly different.

Im trying to use RESTful API (https://home-assista...opers/rest_api/) for home assistant to make a minecraft button turn on a light in real life

So i'll be trying to call http://192.168.5.149.../switch/turn_on

{"entity_id": "switch.christmas_lights"}

Thanks,
Jonny
Edited by jonny190, 11 July 2017 - 10:30 PM.
Quote

Lignum's Photo Lignum 11 Jul 2017

That's fairly straight forward. Simply make a table containing the request data, encode it, and then pass it as the POST body to http.post:
local request = {
  entity_id = "switch.christmas_lights"
}

local response = http.post("http://192.168.5.149:8123/api/services/switch/turn_on", json.encode(request))
response.close()
Quote

CRUGG's Photo CRUGG 31 May 2019

Hey there. I have a little Problem (it's probably something on my side), but could anyone help me?
packageUrl = http.get("https://ploxion.github.io/plx-ppm-packages/packages.json").readAll()
packageObj = json.decode(packageUrl)
value = packageObj[0].name
print(value)
This code doesn't work. What this should do is to print the name of the first Element in the JSON (https://ploxion.gith...s/packages.json)
Quote

SquidDev's Photo SquidDev 31 May 2019

View PostCRUGG, on 31 May 2019 - 09:02 AM, said:

This code doesn't work. What this should do is to print the name of the first Element in the JSON (https://ploxion.gith...s/packages.json)
The top level object is an object, rather than an array - you'd need to do packageObj.luaide.name.
Quote

CRUGG's Photo CRUGG 31 May 2019

View PostSquidDev, on 31 May 2019 - 09:14 AM, said:

View PostCRUGG, on 31 May 2019 - 09:02 AM, said:

This code doesn't work. What this should do is to print the name of the first Element in the JSON (https://ploxion.gith...s/packages.json)
The top level object is an object, rather than an array - you'd need to do packageObj.luaide.name.
Well, the problem with that is, that I want to go through all things with a for loop later. And the problem is, with an array I can't store any sub information. Is there any other way?
Quote

Bomb Bloke's Photo Bomb Bloke 01 Jun 2019

Sounds like you're after a pairs loop.
Quote

CRUGG's Photo CRUGG 01 Jun 2019

I don't really understand the link you sent me...
Quote