C++Language FeatureLearn C++

You NEED to Learn To Use JSON (JavaScript Object Notation)

What is JSON ? How we can test a JSON script? How we can use JSON with C++ Builder? Can we generate and parse JSON in C++ Builder? Let’s answer all in this post.

JSON (JavaScript Object Notation) is a language independent lightweight data-interchange format. JSON can be used as an alternative to other data-interchange formats such as XML or YAML. RAD Studio provides JSON frameworks that contain classes and methods to store, parse, read, write, and generate data in JSON format. Sooner or later you will find it is necessary to understand what JSON is and how to use it since it’s an extremely common format, especially for things like REST servers.

JSON Frameworks

RAD Studio provides different frameworks to handle JSON data.

JSON Example

For example you have animals in a Zoo and you want to hold data in JSON form. In our small zoo, let’s have two Penguins from different origins

  • First one is from Magascar at the age of 4
  • Second one is from Antarctica at the age of 5

In JSON form this data can be written in animals category with their properties as below,

You can test this JSON with a JSON Parser Online , here below is from http://json.parser.online.fr/

JSON Generator Example in C++ Builder

In C++ Builder, we can create this JSON form by using TJSONObject and TJSONArray. We can add properties of animals by the AddPair() method and we can use AddElement() method to add these animals with their features. Let’s do this in a new C++ Builder Project.

  1. Create a new C++ Builder Multi-Device application, save all project and unit files to a folder.
  2. Add a TMemo and two TButtons with text “Generate” and “Parse”
  3. To use JSON methods, we need to include System.JSON .hpp, with #include <System.JSON.hpp> as below,

4. To create this JSON form, first we should create a jobject with TJSONObject and jarray with TJSONArray then we can create elements with TJSONObject to define each animal properties. We can add these elements to our array by using AddElement() method. Here is a simplified code below,

After using JSON object we should free this, all it’s elements will be free too. To make safe data addition, we can use __try and__finally. Here is a simple example to show how we can add all elements safely as below.

If we run this example with a C++ Builder VCL or FMX application, we will have same JSON result.

JSON Parser Example in C++ Builder

We can parse each objects and elements of arrays as given example below. Let’s get this JSON output above from the Memo component and parse.

Here is the example output,

You can check Official DockWiki http://docwiki.embarcadero.com/ for more details and latest news about JSON format. Also don’t forget to check JSON.org for more details about JSON format. If you still have questions JSON in C++ Builder and Delphi is well explained here ChapmanWorld with examples.


Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

33+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux and some other operating systems. Dr. Yilmaz Yoru was born in 1974, Eskisehir-Turkey. He graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. One year later he started to work in the same university as an assistant. He received his MSc and PhD degrees from the same department of the same university. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics and Artificial Intelligence. He also likes the graphical 2D & 3D design and high-end innovations.
Related posts
C++Introduction to C++Language FeatureLearn C++

How To Generate A Random File Name on Windows

C++Introduction to C++Language FeatureLearn C++

How To Include Or Exclude Trailing Backslashes On Windows

C++Introduction to C++Language FeatureLearn C++

How To Check Characters of File Names and Paths on Windows

C++Introduction to C++Language FeatureLearn C++

How to use SameFileName method on Windows