NGINX cjson lua decode json

3.8k views Asked by At

I am trying to run the cjson decode example found in the manual :

and I can't seem to get it to work.

Here is the relevant config:

 location / {
            content_by_lua '
                    local cjson = require("cjson")
                    json_text = '[ true, { "foo": "bar" } ]'
                    value = cjson.decode(json_text)
                    ngx.say(value)
            ';

            resolver 8.8.8.8;
    }

I get this error :

sudo nginx -t
nginx: [emerg] unexpected "[" in /etc/nginx/nginx.conf:29
nginx: configuration file /etc/nginx/nginx.conf test failed

if I remove the square brackets I get:

sudo nginx -t
nginx: [emerg] directive "content_by_lua" is not terminated by ";" in  /etc/nginx/nginx.conf:28
nginx: configuration file /etc/nginx/nginx.conf test failed

Does anyone have any idea what I am doing wrong ? It seems like cjson should be a fairly easy way to parse json in NGINX and I know people are doing it ... just can't figure out why even the example code isn't working for me.

0

There are 0 answers