0

I am using a AT commands on a modem to pass JSON packets to an MQTT broker. The AT command to publish a string uses commas to separate the parameters passed to the command, so when I send

AT#MQPUBS=1,topic_2,0,0,{"Name":"Andrew"},{"Location":"UK"},{"Age":"51"}

I get an error.

Does the JSON standard allow the comma separator to be swapped with another character?

Roberto Caboni
  • 7,252
  • 10
  • 25
  • 39
Andys
  • 43
  • 6
  • JSON does not. Is there a way to escape commas that should be considered as part of an argument, rather than separating arguments? – chepner May 05 '21 at 18:45
  • I have asked the Modem manufacturers the very same question. – Andys May 05 '21 at 18:47
  • I wrote an answer before realizing that the real problem is that the sent message contains double quotes. Otherwise (that was the topic of my answer) enclosing the mesasge would have been enough. – Roberto Caboni May 05 '21 at 19:17
  • In the future it will be possible to escape ascii chars inside string parameters (enclosed by double quotes) with something like \22 – Roberto Caboni May 05 '21 at 19:27
  • The modem command handler for AT#MQPUBS appears to validate the number of parameters passed by counting commas. I need to find a way of disguising the comma between the two elements. Everything after the 4th comma is the message string. – Andys May 05 '21 at 20:58
  • No. If the `message` parameter was `"{Name:Andrew},{Age:51}"` you would have been fine, because Telit parsers ignore commas enclosed by double quotes (trust me ;) ). This makes `"` char the forbidden one. – Roberto Caboni May 06 '21 at 04:51
  • Ok, but json requires the strings within {} to be in quotes, it's it possible to escape these quotes? – Andys May 06 '21 at 06:18
  • Unfortunately, as I wrote above, not yet. There are actually some tricky ways to work this around, but they would require you writing your custom command with AppZone. – Roberto Caboni May 06 '21 at 06:56
  • Ok, I will do some research on AppZone – Andys May 06 '21 at 07:00

1 Answers1

1

You are asking the wrong question, JSON syntax is not the main problem here. Your problem is AT command syntax.

AT#MQPUBS=1,topic_2,0,0,{"Name":"Andrew"},{"Location":"UK"},{"Age":"51"}

This is in absolutely no way valid AT command syntax. AT commands have only two kinds of parameters, numbers and strings. Strings should ALWAYS be enclosed with double quotes at the beginning and end. From the V.250 standard (which is the AT command standard that everyone working with AT commands needs to read):

5.4.2.2 String constants ... String constants shall be bounded at the beginning and end by the double-quote character ... The double-quote character, used as the beginning and ending string delimiter, shall be represented within a string constant as "\22".

Thus the topic_2 argument above is not at all a valid string without the surrounding quotes. If you read the documentation for the AT#MQPUBS command carefully it should state that the second argument is of type string. And looking at it the first argument is actually also a string.

To convert the whole command line into valid syntax it should be

AT#MQPUBS="1","topic_2",0,0,"{\22Name\22:\22Andrew\22},{\22Location\22:\22UK\22},{\22Age\22:\2251\22}"

although at that point you might start looking into JSON syntax because I assume you rather should have "{{\22Name\22: ... :\2251\22}}".


PS Also note that almost all strings are subject to AT+CSCS conversion (there exists a few exceptions, but then the documentation should explicitly say so, and AT#MQPUBS is not such an exception).

hlovdal
  • 26,565
  • 10
  • 94
  • 165