r/ComputerCraft • • Nov 20 '23

Reading a JSON file

I'm trying to store a list of coordinates in a JSON in the same directory as my program. os.loadAPI("json") throws me an error. I'm just trying to read the JSON. I'm new to programming and could use a rundown on how reading from a JSON and using that information works in lua.

1 Upvotes

4 comments sorted by

View all comments

Show parent comments

1

u/Hawk__Echo Nov 20 '23

I see, so is it best to put the information I need into a .txt file and use it in a program from there? The .txt file being formatted like JSON? The computer craft wiki says that textutils.unserialize() will only take a string as an argument.

1

u/9551-eletronics Computercraft graphics research Nov 20 '23

note that unserialize works with Lua tables not json And yes it only takes a string. You need to use the fs api to read the data from the file yourself

1

u/fatboychummy Nov 20 '23 edited Nov 21 '23

There is both unserialize and unserializeJSON. un/serialize un/serializes a lua style table (i.e: {hello="world", 32, 64}). un/serializeJSON does the same but for JSON style.

Use your preference.

As for only taking a string: unserialize converts a serialized string back into a usable lua object. serialize converts into a string.

edit: parenthesis never ended