←  APIs and Utilities

ComputerCraft | Programmable Computers for Minecraft

»

JSON API v2.0.1 for ComputerCraft

ElvishJerricco's Photo ElvishJerricco 10 Nov 2012

I've written a JSON parser! Simply give it a JSON block and it returns a lua value. Just use os.loadAPI() to load it up. Then call json.decode(). This is really useful because a lot of http servers (for example, the github api) use JSON to transmit data. So now you can actually use that data!

Example:

os.loadAPI("json")
str = http.get("http://www.someserver.com/").readAll()
obj = json.decode(str)
value = obj.thisVariableWasInTheJSONAndThisIsCoolerThanUsingStringGmatchToFindEverything

jsonstring = json.encode(obj)
sendThisToWhateverNeedsIt(jsonstring)

-- Also, it is very good for very readable config files.
prettystring = json.encodePretty(obj)
saveThisToAFile(prettystring)

And now introducing json.encodePretty(). Let's say you want the text to be pretty instead of small. json.encodePretty() will return formatted text instead of compact text. This is useful for things like config files where you want your users to be able to read the json easily.

Finally, json.decodeFromFile(). If you have some JSON in a file, this function will read it for you without you having to open the file and read it manually. Just do
local object = json.decodeFromFile("path/to/file")

pastebin get 4nRg9CHU json

Changelog:
Spoiler
Quote

gknova61's Photo gknova61 10 Nov 2012

Nice job, guy! =D
Quote

ElvishJerricco's Photo ElvishJerricco 10 Nov 2012

View Postgknova61, on 10 November 2012 - 04:30 PM, said:


I can't be sure but you appear to be trying to be condescending, as if what I've done is worthless because it's been done before... I guess people can't make their own implementations of anything anymore... But seeing as nothing like this is on the CC forums I figured I'd be alright to post it.
Quote

zekesonxx's Photo zekesonxx 11 Nov 2012

-snip-

Fixed, nevermind.
Quote

Kilobyte's Photo Kilobyte 14 Nov 2012

hey, mind if i add this to KilOS? (link is in my signature and ofc i'll credit you)
also this might better fit into the apis category :P/>
Quote

Orwell's Photo Orwell 14 Nov 2012

I think you did a great job on this. It seems to be quite good. Even if it has been implemented before, there are numerous reasons to make it again. The most important one for me is simply developing your own skill set.
Quote

ElvishJerricco's Photo ElvishJerricco 17 Nov 2012

View PostStiepen, on 14 November 2012 - 10:47 AM, said:

hey, mind if i add this to KilOS? (link is in my signature and ofc i'll credit you)
also this might better fit into the apis category :)/>

I don't mind you using this at all. I actually quite like the KilOS concept (UNIX and stuffs) so yea go ahead and use it.
And yea i realize this would have been better suited there but it's too late now.

View PostOrwell, on 14 November 2012 - 11:24 AM, said:

I think you did a great job on this. It seems to be quite good. Even if it has been implemented before, there are numerous reasons to make it again. The most important one for me is simply developing your own skill set.

I made sure to build it to the best of my ability. The only info i had was json.org which merely explains the format =P
Quote

Kilobyte's Photo Kilobyte 17 Nov 2012

View PostElvishJerricco, on 17 November 2012 - 11:28 AM, said:

I don't mind you using this at all. I actually quite like the KilOS concept (UNIX and stuffs) so yea go ahead and use it.
And yea i realize this would have been better suited there but it's too late now.
Will be in next version
Quote

Orwell's Photo Orwell 17 Nov 2012

View PostElvishJerricco, on 17 November 2012 - 11:28 AM, said:

*snip*
I made sure to build it to the best of my ability. The only info i had was json.org which merely explains the format =P
I made a JSON parser myself once, and I used the RFC: https://www.ietf.org/rfc/rfc4627.txt . Is this more detailed than the info you had? :)/>
Quote

ElvishJerricco's Photo ElvishJerricco 17 Nov 2012

View PostOrwell, on 17 November 2012 - 02:07 PM, said:

View PostElvishJerricco, on 17 November 2012 - 11:28 AM, said:

*snip*
I made sure to build it to the best of my ability. The only info i had was json.org which merely explains the format =P
I made a JSON parser myself once, and I used the RFC: https://www.ietf.org/rfc/rfc4627.txt . Is this more detailed than the info you had? :)/>

They both just describe the format. Neither's really more detailed. I was just pointing that out because I know there's a lot of people on the CC Forums that need to copy paste a lot of their code. I don't like to do that kind of programming.
Quote

CoolisTheName007's Photo CoolisTheName007 08 Dec 2012

Will test this later, seems like it's a good way to make readable config files!!! :D
Quote

ElvishJerricco's Photo ElvishJerricco 01 Feb 2013

Updated. I added encodePretty. It's really useful for config files (that's why I made it)
Quote

Skullblade's Photo Skullblade 01 Feb 2013

not a big deal but FYI this should be in the APIs section ;)
nice job though
Quote

ElvishJerricco's Photo ElvishJerricco 01 Feb 2013

View PostSkullblade, on 01 February 2013 - 03:58 PM, said:

not a big deal but FYI this should be in the APIs section ;)
nice job though

Yea I wasn't thinking when I posted this. Not sure how to move it though =P
Quote

theoriginalbit's Photo theoriginalbit 01 Feb 2013

View PostElvishJerricco, on 01 February 2013 - 04:05 PM, said:

Yea I wasn't thinking when I posted this. Not sure how to move it though =P
Click the report button and say something like "Posted in wrong section, please move to API's and Utilities"
Quote

ElvishJerricco's Photo ElvishJerricco 01 Feb 2013

View PostTheOriginalBIT, on 01 February 2013 - 04:06 PM, said:

View PostElvishJerricco, on 01 February 2013 - 04:05 PM, said:

Yea I wasn't thinking when I posted this. Not sure how to move it though =P
Click the report button and say something like "Posted in wrong section, please move to API's and Utilities"

Ah. Thanks
Quote

Lyqyd's Photo Lyqyd 01 Feb 2013

Moved to APIs and Utilities per user request.
Quote

ElvishJerricco's Photo ElvishJerricco 12 Jun 2013

New update v2.0: Completely rewrote the encoder. Much simpler now. The decoder has been coded a bit more smartly, and it handles the technically illegal null value. There is now a decodeFromFile function. Oh and I should mention it's half as long now because of the encoder rewrite. Went from 396 lines to 191
Quote

Martin2789's Photo Martin2789 11 Feb 2014

Nice Api you have there i get it to work with easy json like:
		{
			"firstline": "John",
			"secline": "Doe",
			"txtcol": "colors.white",
			"backcol": "colors.blue"
		}
with
os.loadAPI("json")
str = http.get("http://www.someserver.com/").readAll()
obj = json.decode(str)
print(obj.firstline)
but with complicated json like:

{
	"monitornorth": [
		{
			"firstline": "John",
			"secline": "Doe",
			"txtcol": "colors.white",
			"backcol": "colors.blue"
		}
	]
}

os.loadAPI("json")
str = http.get("http://www.someserver.com/").readAll()
obj = json.decode(str)
print(obj.monitornorth.firstline)
What am i do wrong?
EDIT:

View PostLBPHacker, on 13 February 2014 - 12:51 PM, said:

Take a look at that piece of JSON again. monitornorth is an array with one object in it. That object is the first thing in monitornorth, so this is how you access it:
print(obj.monitornorth[1].firstline)
Thank you LBPHacker ;)
Edited by Martin2789, 14 February 2014 - 11:19 AM.
Quote

daphee's Photo daphee 20 Jun 2014

Thank you for this nice lib. I've used it successfully on numerous projects.
This time it fails to decode this string
{"success":true,"code":0,"error":"","data":{"clientId":"testClient","requestId":"1","data":{"method":"get_redstone","side":"right"}}}
It fails with "json:167: attempt to index ? (a nil value)"
Quote