C++Language FeatureLearn C++

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

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 necessary to understand what JSON is and how to use it since it’s an extremely common format, especially for things like REST servers.

In this post, you’ll learn what JSON is, How we can test a JSON script? How we can use JSON with C++ Builder? Can we generate and parse JSON in C++ Builder? By learning more about JSON, it will help you build C++ applications with the use of C++ software.

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.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Introduction to C++Learn C++

Learn Copy Constructors in C++ Classes

C++C++11C++14C++17Introduction to C++Learn C++Syntax

Learn How To Use Types Of Destructors In C++?

C++C++11C++14Learn C++Syntax

How To Convert u32string To A wstring In C++

C++C++11C++14C++17C++20Introduction to C++Learn C++

How To Learn The Move Constructors In Modern C++?