Logic Machine Forum
strange backslash when json.write - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Gateway (https://forum.logicmachine.net/forumdisplay.php?fid=10)
+--- Thread: strange backslash when json.write (/showthread.php?tid=4181)



strange backslash when json.write - Hadeel - 08.08.2022

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
?>



RE: strange backslash when json.write - admin - 08.08.2022

No need to remove this backslashes. This is allowed by the JSON specification. Some libraries escape forward slashes and some do not.


RE: strange backslash when json.write - Hadeel - 08.08.2022

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 ?


RE: strange backslash when json.write - admin - 08.08.2022

What programming language/library is your client using? Nearly all of them can handle such escape sequence.


RE: strange backslash when json.write - Hadeel - 09.08.2022

Thank you admin! I think it's Go lang


RE: strange backslash when json.write - admin - 09.08.2022

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)



RE: strange backslash when json.write - Hadeel - 10.08.2022

Thank you admin !!

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