Link Search Menu Expand Document

Common Mistakes and Gotcha Part 2

Common Mistakes and Gotcha - Secrets of JSON

In this JSON file, can you tell me what kind of mistakes I made? You can observe the mistake which I committed in this file. One mistake I did if you will notice, you can see that I forgot to add the comma (,). Now let’s go through all these mistakes and fix them using our online JSON validator tool provided by ByteScout.

I have pasted our disease history JSON file content and when I paste that JSON file in this tool, it immediately starts throwing errors. It is showing that I have forgotten the comma, so add this comma. After adding the comma, it started showing me the next error at line number 10. Can you identify what kind of mistake it is?

Consider a situation like if you have a control character within a string literal in your JSON file. For example, this literal String contains a new line fit character. What to do in this situation? Can you fix this issue? You have to use a new line character which is backward slash and n and let me arrange in one line (“Disease”: “Antibiotic-resistant\n Infections”,).

Common Mistakes and Gotcha

You can see that we have fixed this new line character issue and it started showing me another issue which is at line number 16. Again consider a situation that if any of this JSON property value contains a literal backward slash character (“ConsultTo”: “Dr\. Peter Jones”). Right now it is giving a Syntax error because it is trying to Parse a String with backward slash and dot.

How can we fix this issue? To fix this issue you need to escape it by using one more backward slash character. It is showing me the next error which is at line number 17. What is the error at line number 17? Can you identify it? I have added the extra comma over here. Remove the extra comma and that error is gone.

After that, it is showing me the next error which is at line number 23. Here at line number 23, can you identify what I have forgotten? I have forgotten the colon. Instead of the dash, I need to add colon over here and if I scroll up then that error is gone. Then it started showing me the next error which is at line number 27. What is in line number 27? Now again can you tell me what mistake I made over here?

Let me remind you one thing once again, in JSON you have to have double quotes around the key name and this is the thumb rule. I guess that you got it by now. I forget the double quotes over here. Then it is showing the next error at line number 29. Can you help me with what kind of mistake I made over here?

As we know that in JSON, Arrays always start and end with a square bracket. We have opened the square bracket over here but instead of adding the square bracket, I have added the curly brace. To fix this issue I need to add a closing square bracket. You can see that our JSON file has become a completely valid JSON.

Common Mistakes

Let's move ahead and consider a situation: what if my JSON file contains any comment (// patient information)? As you can see that this tool has now started throwing this type of exception. It means comments are not allowed in the JSON file. Let me show you one StackOverflow thread for this. Here you can see that comments were removed from JSON by design.

According to this answer, JSON does not support comments and it was also never intended to be used for configuration files where commands would be needed. Let me show you another StackOverflow thread. According to this StackOverflow thread nowadays some of the libraries like JSON.net started supporting comments in JSON files. In this answer as you can see that JSON.net version 6.0 supports single-line comments.

After this session, I hope you will take care of all these points which we have learned when working with JSON, and with this, we have reached the end of the module.

Conclusion

In this course, we have learned that JSON is a lightweight, open standard, and human-readable text. It is based on a subset of JavaScript which is good as it is supported natively by JavaScript. JSON uses Key/Value Pairs, Arrays, and Objects to represent data. One of the advantages of JSON is that it has types like Numbers, Strings, Booleans, etc which makes it very convenient and easy to use.

For example, you can represent numbers differently from Strings which is great but there is no date and time type. Dates are hard and can be represented in different ways. Then we have created our JSON file and manipulate the same using Edgex. We have also seen the real-time use cases of JSON in REST API’s.

Finally, we talked about some of the common mistakes which developers may make when working with JSON and how to fix some of the gotchas in JSON. JSON is a useful skill to have on your belt. It is something that you can use on a wide variety of development projects.

Congratulations! You are now familiar with JSON. You should feel confident tackling problems related to JSON and using it in your production application. I really appreciate your time and I hope this course will be useful for you.

ByteScout academy provides many other free courses which you might be interested in. For example, if you are interested in SQL server then you can go for this course “SQL Server For Beginners”. If you are interested in data extraction from PDF documents then you can also explore this “PDF Extractor SDK Essentials” course.

Common Mistakes JSON

If you are interested in decoding barcodes from Images, PDF, and Tiff documents then this course is for you. We have also created courses on different topics and publish them on different platforms like Youtube, Udemy, Dailymotion, Vimeo, and so on. You can find all our course links in the description section below.

Let's say if you are interested in regular expression courses then you can find them on the Youtube on ByteScout channel. Same way if you want to learn the basics of PDF then you can go for this PDF course which is “Essential Secrets of PDF”.

Other useful articles:


Back to top

© , PDF to JSON — All Rights Reserved - Terms of Use - Privacy Policy