• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

get only a specific json object using Gson

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everyone
so a web API is sending me a huge json file which most of it is useless to me, thus using JsonReader I'm trying to get only the required value.
But JsonReader after reading all values of the first object instead of traversing to next object finishes the process.
Here is part of my json (edited)

I'm trying to access "def" object members, more specifically second member of "dt".
I assume this should have a standard solution but I'm unable to find it.
this is my current (not working) code

Any idea how to solve this?
thank you
 
Saloon Keeper
Posts: 10929
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What jar file are you building with?
 
Sheriff
Posts: 4641
582
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The JSON document is mal-formed - it missing a brace near the end.  Corrected:
 
Naadiyaar Mansouri
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:What jar file are you building with?


I'm afraid I don't understand what you mean by jar file
but I'm using gson version 2.11.0 and building the code using maven exec plugin
 
Ron McLeod
Sheriff
Posts: 4641
582
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know what else you might need to do with the document, but when I have a complex document and I just want to extract a single item from it, I often use JsonPath.

With JsonPath you describe a path through the document to the location of the items of interest.  The downside is that is has a significant learning curve, and you can spend a lot of time in the beginning trying to form the expression that fits your needs.  There are on-line evaluators however which can help by showing you the results of the expression as you type (https://jsonpath.com/ and https://jsonpath.curiousconcept.com/#).

It may not be a good solution for what you are working on now, but it might be useful with some future project.

Here's an example (JSON document abbreviated):
Example console output (long lines shortened):
The first expression $[0].def[0].sseq[*][0][1].dt[0][1] breaks-down like this:
   $[0]  first element in document
   def[0]  contents of first occurance of def
   sseq[*]  contents of all occurrances of sseq
   [0]  contents of first container
   [1]  contents of second container (sense)
   dt[0]  contents of first occurance of dt
   [1]  value of second item

The secondexpression $[0].def[0].sseq[?(@[0][1].sn == '2')][0][1].dt[0][1] includes a filter to only include contents where sn has a value of 2.

These are the dependencies that I used:
 
Carey Brown
Saloon Keeper
Posts: 10929
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The hasNext() is returning false. This is my work around.
 
Carey Brown
Saloon Keeper
Posts: 10929
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With indent and BOOLEAN.
 
Saloon Keeper
Posts: 28319
210
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm with Ron.

Two reasons:

1. Less code overall and the code is more to the point
2. A streaming reader is likely to be more memory efficient, since it doesn't have to load the entire document into RAM.

The jsonpath filter expression language is just one example of a path-seeking language. Another popular one it XML's xpath. Python's BeautifulSoup path processing also works on that basis for html, although it's document-based, not stream-based.

JSON complex data elements are one of two forms: dictionaries and arrays, and the jsonpath language reflects that. One good thing about JSON is that your browser might be able to view it with expand/collapse text, which makes it easier to home in on the elements of interest. So it needn't be that big a struggle.

 
Tim Holloway
Saloon Keeper
Posts: 28319
210
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Three reasons.

3. If the document structure changes, it's a lot easier to re-code a path than to manually re-code document search logic.
 
Naadiyaar Mansouri
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all for great information!
 
snakes are really good at eating slugs. And you wouldn't think it, but so are tiny ads:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic