Win a copy of Pro Spring MVC with WebFlux: Web Development in Spring Framework 5 and Spring Boot 2 this week in the Spring forum!
  • 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

How can I read and parse such kind of JSON?

 
Ranch Hand
Posts: 2450
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can I read and parse the below kind of JSON ?


{ "any": [
   { "all": [
       { "ge": ["A", 10] },
       { "eq": ["B", 20] }
   ]},
   { "lt": ["C", 30] },
   { "ne": ["D", 50] }
]}

This JSON would be created for conditional operators for sample condition ( A >= 10 && B == 20 ) || ( C < 30 ) || ( D != 50 )

A,B,C,D are elementNames and 10,20,30,40 are elementValues.

What kind of POJO should be created to parse this?

Thanks.
 
Sheriff
Posts: 67537
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any Java JSON library will work on properly formatted JSON.
 
Monica Shiralkar
Ranch Hand
Posts: 2450
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried to check in online JSON to POJO converter and it gives me this kind of POJO:




for this json:

{ "OR": [


What kind of POJO can I use for this parsing (Or do I have to not use any POJO for this and parse it using any other way)?

Thanks
 
Monica Shiralkar
Ranch Hand
Posts: 2450
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry the JSON was not format correctly. The actual JSON is like this

 
Sheriff
Posts: 16240
271
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your other posts, you indicated that you eventually want to emit some SQL based on the JSON, right?

Well, I don't know if you'll find any libraries out there that will do exactly that. At best, I think you might be able to find some libraries that can help you create a Parse Tree for the grammar reflected in the JSON and translate that same parse tree into SQL. Or you could just go with what you have in the standard Java library. Specifically, the Collections API (most of the classes in the java.util package).

That is, you would need to parse from JSON --> get an intermediate output of a Parse Tree (for you boolean grammar) --> then walk through the parse tree to generate the final output, an equivalent SQL statement.

Again, that's a very specific requirement and I think you have your work cut out for you in writing the state machine and do all that.
 
Monica Shiralkar
Ranch Hand
Posts: 2450
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank You.

I think, it was a mistake to think that I need to create POJO for parsing it. My focus was on thinking that what kind of POJO should I create for parsing it whereas the POJO may not be required at all. Now, I am trying to do this using Jackson's built in tree model feature.

I am thinking of making use of such kind of code :



However, the JSON which I want to parse can become complex in case of too much nested code. I will have to probably make a recurrsive logic and form the query step by step using the iterator.
 
Junilu Lacar
Sheriff
Posts: 16240
271
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You seem to be hung up on the term "POJO" - I don't know what you think it means but it's just a Plain Old Java Object, which is to say it's not an Enterprise Java Bean or some other kind of Java class that might require a lot of framework infrastructure and scaffolding around it to be useful. That is, it's just a normal, plain-vanilla Java class.

Depending on how you structure your design, of course it's possible to write a POJO to help you do what you want to do! In fact, if it were me, I would absolutely start out with just a POJO for this.
 
Junilu Lacar
Sheriff
Posts: 16240
271
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:However, the JSON which I want to parse can become complex in case of too much nested code. I will have to probably make a recurrsive logic and form the query step by step using the iterator.


I don't know about recursive logic to parse the JSON. Just because your JSON can have a recursive structure, that doesn't mean your logic to process it has to be recursive as well.

Again, you'll need to write a parser, which is basically a state machine. Study how to write a state machine.  I don't know what your background is but this is something you learn when you study Computer Science.
 
Monica Shiralkar
Ranch Hand
Posts: 2450
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. I could do it as below using Jackson Tree model:

1. Find the root node and keep traversing to the inner most JSON. While doing this keep each JSON and operator into a Stack in this sequence.
2. Start popping elements from stack one by one.
3. Pop the first two elements. this includes inner JSON and operator. Create an intermediate query using this.
4. Find the count of the current stack and keep popping elements two at a time (JSON,operator) in a loop . While doing this form query and append to the intermediate query within a loop. Do all this within the loop.

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic