This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

strange backslash when json.write
#1
Hi !

I am working on LM app development and below is the index.lp code.
From the commandline I can use the endpoint like this , with a request body. It returns the result like this : {"results":[{"alias":"6\/6\/1","value":true}]}
Is there a way to remove the backslash in from the result json string ? 

Thank you !

http://id:pass@LM local IP/apps/data/logic-machine/index.lp
{
    "commands":[
        {
            "alias":"6/6/1",
            "value":true
        }
    ]
}
Code:
<?
  require('json')
  require('apps')
  params = ngx.req.get_body_data()
  params = json.pdecode(params)

  require('custom.utils')

  results = {}
  if params ~= nil then
    for index, command in ipairs(params.commands) do
      if type(command.alias) ~= nil or type(command.value) ~= nil then
        res = grp.write(command.alias, command.value) or false
        table.insert(results, {value = res, alias = command.alias})
      end
    end
    results = {
      results = results
    }
    write(json.encode(results))
  end
?>
Reply
#2
No need to remove this backslashes. This is allowed by the JSON specification. Some libraries escape forward slashes and some do not.
Reply
#3
Thank you admin for your help always !

Oh I understand ... my client want them without backslashes....
Is there any way not to escape this ?
Or maybe I need to ask them to unescape them when they decode json ?
Reply
#4
What programming language/library is your client using? Nearly all of them can handle such escape sequence.
Reply
#5
Thank you admin! I think it's Go lang
Reply
#6
Go JSON parser will handle it correctly.
Escaping slashes can be removed like this but it's a pointless operation Smile
Code:
results = json.encode(results)
results = results:gsub('\\/', '/')
write(results)
Reply
#7
Thank you admin !!

I will talk with my client about this (:
Your solution worked also !
Reply


Forum Jump: