top of page

How to parse Dynamic JSON String?

Article Date: 19-Dec-2021, 5 mins to read. - Swati Patil

I was working on the LMS JOB API Automation project with Numpy Ninja. The LMS Program API supports CRUD operation with API interface with POST, GET, PUT, and DELETE method.

I noticed that LM Program returns the JSON, which is Dynamic.

Let me explain first basics of the JSON:

JavaScript Object Notation (JSON) is a string for representing structured data based on JavaScript object syntax. It is used for transmitting data in sending some data from the server to the client or vice versa.

In a nutshell, it is a

· Data is in the form of Key-Value pair

· It uses JavaScript Object Notation

  • Data is separated by commas

  • Curly braces hold objects

  • Square brackets hold arrays

Let's see below the how JSON looks like:

	"key1": "value",

As you see above, JSON starts with an opening brace "{", and closes with a closing brace "}." It is called a JSON object. JSON contains a predefined structure, called JSON Schema. You will see the key-value pair of "key1" as a JavaScript Object property in the above JSON. In the real world, the JSON Object key can be any object property examples - firstName, lastName, and address. Let's see the actual real-world JSON string:

	{"firstName":"John", "lastName":"Doe"},
	{"firstName":"Anna", "lastName":"Smith"},
	{"firstName":"Peter", "lastName":"Jones"}

The above JSON has three JSON Objects with fixed key names like "firstName", and "lastName" and holds different Person object values.

Let's see another example of holding a JSON object with the Array:

		{"firstName":"John", "lastName":"Doe"},
		{"firstName":"Anna", "lastName":"Smith"},
		{"firstName":"Peter", "lastName":"Jones"}

The above JSON string holds the "Persons" object with fixed or predefined JSON Object properties for each person.

I have explained to you the basics of JSON. Let me explain to you - What is the Dynamic JSON?

The dynamic JSON string is a string that does not have fix key name. It isn't easy to understand. Let me explain you with Example:

            {“1”: “John”},
            {“2”: “Anna”},
            {“3”: ...}
            {“1”: “Doe”},
            {“2”: “Smith”},
            {“3”: ...}

The above JSON file is a dynamic growing JSON. Let me explain to you. You see the "personFirstName" and "personLastName" with growing array of keys "1", "2", "3", etc. I know it isn't easy to understand.

Let me show you the LMS JOB API JSON string. I am using an online JSON formatter tool.

The LMS JOB API returns the root JSON Object, which is “data”, and seven properties like “Job Title”, “Job Company Name”, “Job Location”, “Job Type”, and etc.

If you expand each JSON property, the keys are dynamic. The Keys are "1", "2", "3" and etc.

You can see “Job Title” and “Job Company Name” properties below. I hope you understood what Dynamic JSON means.

Did you notice anything in the JSON string?

I noticed, each JSON Property key is gowning or incrementing by one (1). I started thinking about the structure of data. It is a kind of Array. But I notice, I cannot use Array as Arrays are fixed size, and resize of is a costly operation.

Let me explain to you What is meant by the fixed size of Array. Let’s see the Array Syntax:

String[] jobTittle = new String[10]; 

If you see the above Syntax, you need to pass the max number of elements that can hold by the "jobTitle" property. I am giving number ten at the time of initializing the array variable.

I started my research on the data structure that can hold dynamic growing data. I came across the “LinkedList”, “List” data structure to store the dynamic content. But the “LinkedList” and “List” do not hold key and Value pairs. I finally concluded to use HashMap, which can store the list of keys and values in the memory.

I started modeling my Java classes using HashMap to deserialize the JSON file. I used the Jackson library in my code.

What is meant by deserializing JSON string? Serialization typically means converting objects into JSON string. Deserialization is the reverse process, converting JSON string into an object.

You can see the model classes and utility function below: class:

Data is the root JSON property in the LMS Job API. The Data class holds the "jobTitle", "jobCompanyName", ""jobLocation", "jobType", "jobPostedDate", "jobDescription", and "jsonID" JSON properties.

You will see the below JobTitle class which holds the “jobTitle” property using HashMap and JSONAnySetter property. The Jackson library provides “JSONAnySetter” annotation and internally deserializes the JSON String to Object.

I used a similar approach was used for the remaining data properties. You will notice in the below Java classes.

JobInfo is the plain old Java object (POJO) model class to return the list of LMS Job objects.

Utility Method:

The below utility method converts JSON String to the data object, iterates the based on the KeyID index in the FOR loop, and returns the JobInfo List object.

You can deserialize dynamic JSON strings via Hashmap in the Java code.

14,291 views2 comments

Recent Posts

See All
bottom of page