Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 839427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T05:27:42+00:00 2026-05-15T05:27:42+00:00

Simple situation – read a json file discover all key-value pairs compare key-value pairs

  • 0

Simple situation –

  1. read a json file
  2. discover all key-value pairs
  3. compare key-value pairs

I tried gson, package from json.org, but can’t seem to get far with it.

Can someone please provide a clear sample in Java on how to take a file, read it, end up with json objec I can get key/value pairs from.

Consider this:

private void runThroughJson(JsonObject jsonObject) {
    for (final Entry<String, JsonElement> entry : jsonObject.entrySet()) {

        final String key = entry.getKey();
        final JsonElement value = entry.getValue();

        System.out.println(key + " - " + value);

        if (value.isJsonObject()) {
            runThroughJson(value.getAsJsonObject());
        } else {                
            int ix = value.getAsString().indexOf('[');
            int ig = value.getAsString().lastIndexOf(']');

            System.out.println(ix);
            System.out.println(ig);

            String a = value.getAsString().substring(ix, ig);
            JsonElement jsonElement = parser.parse(a);
            runThroughJson(jsonElement.getAsJsonObject());
        }
    }
}

Logically, it seems alright, however, i get an exception:

Exception in thread "main" java.lang.IllegalStateException
    at com.google.gson.JsonArray.getAsString(JsonArray.java:133)
    at com.cme.esg.bk.TryGson.runThroughJson(TryGson.java:46)
    at com.cme.esg.bk.TryGson.runThroughJson(TryGson.java:44)
    at com.cme.esg.bk.TryGson.goForIt(TryGson.java:32)
    at com.cme.esg.bk.TryGson.main(TryGson.java:16)

Can you please advise that am i missing.

  • 1 1 Answer
  • 3 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T05:27:42+00:00Added an answer on May 15, 2026 at 5:27 am

    With Gson (assuming that you have on object {...} on the top level of your json file):

    final JsonParser parser = new JsonParser();
    final JsonElement jsonElement = parser.parse(new FileReader("/path/to/myfile"));
    final JsonObject jsonObject = jsonElement.getAsJsonObject();
    
    for (final Entry<String, JsonElement> entry : jsonObject.entrySet()) {
       final String key = entry.getKey();
       final JsonElement value = entry.getValue();
       ....
    }
    

    In response to your comment:

    You should certainly avoid re-parsing the json from a string. Use something like:

    ... else if (value.isJsonArray()) {
       final JsonArray jsonArray = value.getAsJsonArray();
       if (jsonArray.size() == 1) {
          runThroughJson(jsonArray.get(0));
       } else {
            // perform some error handling, since
            // you expect it to have just one child!
       }
    
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

All, my situation is that I have the basic route, plus some other simple
Simple situation... With any random XML file, I want to create a list of
I have a simple parent-child situation where the parent can have multiple children. The
The situation is very simple, I have two panel. In the event of OnMouseOver
Situation: I have a simple XML document that contains image information. I need to
Here's the situation: I'm developing a simple application with the following structure: FormMain (startup
I have a situation in C# where I have a list of simple types.
I've come across a situation where I try to run a simple line of
I have quite a simple situation. I've got 3 objects: an object called TrainingConsultant
I have a simple situation. A large organisation is using several different versions of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.