Using strstr to Parse JSON Messages

When I first started to write embedded applications in C, microcontrollers were not very powerful, and we rarely ever parsed or worked with human readable text. Instead, we often generated packetized data where each byte in the message represented some specific type of information that we would then quickly decode on the fly as it was needed. If we watched bus traffic, we would have to remember or have a cheat sheet to tell us what bits and bytes we were interested in watching. Many products today though are connected to the internet and exchange JSON messages which are completely human readable and are parsed in a completely different way. In this post, I’m going to demonstrate a simple way to parse JSON messages using the C library function strstr.

An Introduction to strstr

strstr is a C library function included in string.h that searches a string for a replica of another string. This can be extremely useful to parse a JSON packet assuming you know that you are looking for values in the message like:

  • “LED1_ON”
  • “LED1_OFF”
  • “MOTOR_ON”
  • Etc

A developer can easily setup a series of if/else if/else statements to look for values in the JSON. For example, if I know that I am expecting a JSON messages that have an attribute/value pair of:

{

   “message” : “LED1_ON”

}

Or

{

   “message” : “LED1_OFF”

}

I can write a simple if block that checks for these command messages as follows:

if(strstr(JSON_Buffer, “LED1_ON”) != NULL)

{

   // Code to turn LED1 on

}

else if(strstr(JSON_Buffer, “LED1_OFF”) != NULL)

{

   // Code to turn LED1 off

}

else

{

   // Code for an unknown command or additional commands

}

Simplifying strstr Parsing with for loops

In the above code, JSON_Buffer is a string buffer that contains the received JSON message. We are using strstr to search this buffer for known command values that our embedded applications are interested in knowing about. We then have a string that we are searching the buffer for. If strstr returns NULL, it means that it did not find our string of interest. If it isn’t NULL, then it returns a pointer to the first occurrence of the string we are searching for in the JSON_Buffer.

Using if/else if/ else statements to parse JSON messages for strings of interest is useful but if there are dozens of strings we are interesting in searching for this can get a bit out of hand rather quickly. An alternative is to instead create a table of strings and then use a for loop or a for loop. For example, we can use a while loop that would look something like the following:

the code might look something like the following:

for(Index = 0; (Index < MaxIndex) || (Result != NULL); Index++)

{

   Result = strstr(JSON_Buffer, &StrCommandList[Index])

}

Once you know what index the string is at, you can then execute a function pointer associated with the string to execute a function for the command. Just make sure that you verify that the function pointer is not NULL before dereferencing it.

Conclusions

In the past, developers used to be able to ignore the C libraries string functions because we didn’t work with strings! However, that has now dramatically changed since many developers are now working with IoT connected devices and these devices pass human readable strings back and forth using JSON. There are many ways to parse these messages but strstr is a quick and simple method that developers can start with. More sophisticated parsing algorithms can then be implemented later as they are needed.

Share >

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.