DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Jackson vs Gson: Edge Cases in JSON Parsing for Java Apps
  • JSON Handling With GSON in Java With OOP Essence
  • Let's Unblock: Read Json Using GSON in Scala
  • Stop Poisoning Your Models: How I Built a CV Dataset Quality Toolkit I Can Reuse Forever

Trending

  • You Don't Get to Retrofit Trust: Why API Security Must Be Designed In, Not Bolted On
  • How AI Is Transforming Software Engineering and How Developers Can Take Advantage
  • A Scalable Framework for Enterprise Salesforce Optimization: Turning Outcomes Into an Operating System
  • The Update Problem REST Doesn't Solve
  1. DZone
  2. Coding
  3. Languages
  4. JSON Manipulation Using GSON

JSON Manipulation Using GSON

Let's explore a JSON manipulation using GSON.

By 
Munish Bansal user avatar
Munish Bansal
·
Aug. 15, 19 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
22.3K Views

Join the DZone community and get the full member experience.

Join For Free

There are various libraries for parsing and manipulating JSON, like objectMapper, net.sf, and GSON. We will be discussing the JSON library in-depth.

You can add the GSON dependency through Maven:

<dependency>
  <groupId>com.google.code.gson</groupId>
  <artifactId>gson</artifactId>
  <version>2.8.5</version>
</dependency>

Let's take the below JSON as an example:

{
"name": "John",
"address": {
"city": "London",
"plot": 12
},
"age": 23,
"registered": true,
"isEligible": false,
"hobbies": ["music", "tech news", "blog"],
"birthMark": null
}

We parse the JSON string by creating the object of JsonParser and calling its parse method.

String details = "{\"name\":\"John\",\"address\":{\"city\":\"London\",\"plot\":12},\"age\":23,\"registered\":true,\"isEligible\":false,\"hobbies\":[\"music\",\"tech news\",\"blog\"],\"birthMark\":null}";
JsonObject jsonObject = new JsonParser().parse(details).getAsJsonObject();

You wonder what is JsonObject of type. Whenever we parse a JSON string, the object that we get is of type JsonElement. JsonElement in GSON is similar to the object class in Java, which is the superclass of all JSON. It then further extends to the below types:

Image title

A valid JSON that we are going to parse can be a JsonObject or a JsonArray, hence whenever we parse the JSON, we get the object of JsonElement and then we have to use either the getAsJsonObject() or getAsJsonArray() method to get the object of the desired type. We have used getAsJsonObect() method in our case, based on our example.

Below is the example of a valid JSON array and how we can parse it.

String str = "[\"Ford\", \"BMW\", \"Fiat\"]";
JsonArray array = new JsonParser().parse(str).getAsJsonArray();

We can check if any jsonAttribute is null by calling the isJsonNull() method of JsonElement.

if(jsonObject.get("birthMark").isJsonNull()){   
  System.out.println("birthMark is null");
}

Thanks for reading!

JSON Gson

Opinions expressed by DZone contributors are their own.

Related

  • Jackson vs Gson: Edge Cases in JSON Parsing for Java Apps
  • JSON Handling With GSON in Java With OOP Essence
  • Let's Unblock: Read Json Using GSON in Scala
  • Stop Poisoning Your Models: How I Built a CV Dataset Quality Toolkit I Can Reuse Forever

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook