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
Newsletter
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
  • Building a Reddit Scraper in Go: JSON Listings, Pagination, Rate Limits, and Where It Breaks

Trending

  • How to Migrate From C/C++ to Rust: A Practical Guide for Embedded IoT Systems
  • Stop Writing Database Migration Scripts: Declarative Schema Sync for Java
  • Architecture Review Boards for Generative AI: A Framework for Governance, Risk Management, and Responsible Adoption
  • Testing Java and Spring Applications With Kotlin: Low-Risk to Adoption

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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
·
Aug. 15, 19 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
22.5K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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
  • Building a Reddit Scraper in Go: JSON Listings, Pagination, Rate Limits, and Where It Breaks

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • 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