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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

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
  • Streamlining Event Data in Event-Driven Ansible

Trending

  • How Can Developers Drive Innovation by Combining IoT and AI?
  • Understanding IEEE 802.11(Wi-Fi) Encryption and Authentication: Write Your Own Custom Packet Sniffer
  • Integration Isn’t a Task — It’s an Architectural Discipline
  • AI-Driven Root Cause Analysis in SRE: Enhancing Incident Resolution
  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.0K 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
  • Streamlining Event Data in Event-Driven Ansible

Partner Resources

×

Comments
Oops! Something Went Wrong

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!