0

I have attempted to follow the following article and I keep getting module Json module not found.

Convert JSON String to Lua Table?

I am installing Json-lua from luarocks as per the article. Then I am just copying and pasting the code. When I run my lua script I get the following error message.

What am I doing wrong??

root@fusion01:# luarocks install json-lua
Installing https://rocks.moonscript.org/json-lua-0.1-3.rockspec...
Using https://rocks.moonscript.org/json-lua-0.1-3.rockspec... switching to 'build' mode
Cloning into 'json-lua'...
remote: Enumerating objects: 7, done.
remote: Counting objects: 100% (7/7), done.
remote: Compressing objects: 100% (6/6), done.
remote: Total 7 (delta 0), reused 4 (delta 0), pack-reused 0
Receiving objects: 100% (7/7), 16.29 KiB | 0 bytes/s, done.
Checking connectivity... done.
Do not use 'module' as a build type. Use 'builtin' instead.
Updating manifest for /usr/local/lib/luarocks/rocks
json-lua 0.1-3 is now built and installed in /usr/local (license: CC)

root@fusion01:# lua sendjson.lua         
lua: sendjson.lua:21: module 'json' not found:
        no field package.preload['json']
        no file '/usr/local/share/lua/5.2/json.lua'
        no file '/usr/local/share/lua/5.2/json/init.lua'
        no file '/usr/local/lib/lua/5.2/json.lua'
        no file '/usr/local/lib/lua/5.2/json/init.lua'
        no file '/usr/share/lua/5.2/json.lua'
        no file '/usr/share/lua/5.2/json/init.lua'
        no file './json.lua'
        no file '/usr/local/lib/lua/5.2/json.so'
        no file '/usr/lib/x86_64-linux-gnu/lua/5.2/json.so'
        no file '/usr/lib/lua/5.2/json.so'
        no file '/usr/local/lib/lua/5.2/loadall.so'
        no file './json.so'
stack traceback:
        [C]: in function 'require'
        sendjson.lua:21: in main chunk
        [C]: in ?
root@fusion01:# cat sendjson.lua 
#!/bin/lua


--local raw_json_text    = JSON:encode(data)
--local pretty_json_text = JSON:encode_pretty(data)

local json = require "json"

local t = { 
    ["name1"] = "value1",
    ["name2"] = { 1, false, true, 23.54, "a \021 string" },
    name3 = json.null
}

local encode = json:encode (t)
print (encode)  --> {"name1":"value1","name3":null,"name2":[1,false,true,23.54,"a \u0015 string"]}

local decode = json:decode( encode )
root@fusion01:# 
Egor Skriptunoff
  • 23,359
  • 2
  • 34
  • 64
user2236794
  • 441
  • 3
  • 7
  • 22
  • This SO question looks like an "ad opportunity" :-) There are a lot of pure-Lua JSON modules. Even I have [one which works](https://github.com/Egor-Skriptunoff/json4lua). No luarocks needed. Just copy `json.lua` to Lua modules' folder. – Egor Skriptunoff Sep 12 '19 at 04:35
  • Either the package is broken, or you don't have luarocks set up correctly – DarkWiiPlayer Sep 12 '19 at 08:48
  • 1
    Try [Lua CJSON](https://github.com/mpx/lua-cjson/), we are using this too after comparing all the other possible modules. In my opinion it's the fastest one. – csaar Sep 13 '19 at 06:31

0 Answers0