This week's book giveaways are in the Jython/Python and Object-Oriented programming forums.
We're giving away four copies each of Machine Learning for Business: Using Amazon SageMaker and Jupyter and Object Design Style Guide and have the authors on-line!
See this thread and this one for details.
Win a copy of Machine Learning for Business: Using Amazon SageMaker and JupyterE this week in the Jython/Python forum
or Object Design Style Guide in the Object-Oriented programming 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

Grouping JSON response with keys in Java

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am new to java development. I am learning JSON parsing. I got JSON response something like this.



if price match it should give me sizerange back like below
I want to parse this Json into something like this.




How can I achieve this? My motive is to group all the size ranges according to their prices.
 
Saloon Keeper
Posts: 11188
244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your ranges overlap. Without knowing the price, in what range would L be?
 
Marshal
Posts: 6643
177
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you get that sorted out, you should probably use some sort of JSON parser library such as json-simple by Clifton Labs.
 
Saloon Keeper
Posts: 2943
373
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just adding a formatted view of your data so it is easier to visualize what you have:(wrapped your data in { } to make it a proper object)
 
sushobhan bhaumik
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Your ranges overlap. Without knowing the price, in what range would L be?



if prices are same it will fall in one group range
i.e l price is 0.0 and XXL price is 0.00 so in same group range like this.

i just updated price now for clear view
 
sushobhan bhaumik
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ron McLeod wrote:Just adding a formatted view of your data so it is easier to visualize what you have:(wrapped your data in { } to make it a proper object)



ThANKS A LOT
 
sushobhan bhaumik
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anyone please help here.
 
Stephan van Hulst
Saloon Keeper
Posts: 11188
244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, so start by deserializing your JSON data. You need to create classes that mirror your data. If you use JSON-B (a standard for mapping JSON to Java classes) you can then easily convert the data. The JSON-B user guide shows you very clearly how you can do this: http://json-b.net/docs/user-guide.html

After you have deserialized your data, you need to convert your model from a list of prices per size to a list of prices per range. You can easily do this by first grouping all the prices per size by price, and then collecting each group into a range.

Finally, convert your result back to JSON.

Start by writing classes that represent your data, and show them to us.
 
sushobhan bhaumik
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have created a class for this




and in my main class i have put below code

 
Ron McLeod
Saloon Keeper
Posts: 2943
373
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your JSON string representation doesn't look right.  It should be something like:
 
sushobhan bhaumik
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot.but while executing i am getting below exception.

Exception in thread "main" java.lang.NoClassDefFoundError: com/fasterxml/jackson/annotation/JsonView
at com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector.<clinit>(JacksonAnnotationIntrospector.java:35)
at com.fasterxml.jackson.databind.ObjectMapper.<clinit>(ObjectMapper.java:285)
at com.puma.pid.shared.dao.abu.converter.JacksonArrayExample.main(JacksonArrayExample.java:26)
Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.annotation.JsonView
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
... 3 more

I have added dependency like this below

 <dependency>
           <groupId>com.fasterxml.jackson.core</groupId>
           <artifactId>jackson-databind</artifactId>
           <version>2.8.0</version>
</dependency>
 
Ron McLeod
Saloon Keeper
Posts: 2943
373
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

sushobhan bhaumik wrote:Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.annotation.JsonView


The error message is telling you that the @JsonView annotation is being referenced, but the class cannot be found.

Try adding the dependency:
 
Ron McLeod
Saloon Keeper
Posts: 2943
373
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

sushobhan bhaumik wrote:i have created a class for this ...


This is going to be a problem:

    private int cargoHandoverPrice;

    "cargoHandoverPrice":1.66
 
sushobhan bhaumik
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ron McLeod wrote:

sushobhan bhaumik wrote:i have created a class for this ...


This is going to be a problem:

    private int cargoHandoverPrice;

    "cargoHandoverPrice":1.66



so what i should do.?
 
sushobhan bhaumik
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ron McLeod wrote:

sushobhan bhaumik wrote:Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.annotation.JsonView


The error message is telling you that the @JsonView annotation is being referenced, but the class cannot be found.

Try adding the dependency:



After adding this dependency still same error
 
Ron McLeod
Saloon Keeper
Posts: 2943
373
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does 1.66 represent an int?
 
sushobhan bhaumik
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ron McLeod wrote:Does 1.66 represent an int?



sorry it should be double

after adding that dependency i am getting same issue i am using jdk 11
 
Ron McLeod
Saloon Keeper
Posts: 2943
373
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

sushobhan bhaumik wrote:after adding that dependency i am getting same issue ...


Please post your POM and code for the class where you are using ObjectMapper.
 
Ron McLeod
Saloon Keeper
Posts: 2943
373
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

sushobhan bhaumik wrote:i have created a class for this ... and in my main class i have put below code ...


First, remove all those extra blank lines - it makes your code hard to read.

Next, I would rename SizePrices to SizePrice since that class only models a single item.

I also think that you should create a SizePricesCollection class which holds the collection of items to make deserializing easier.  For example:
Then you could use ObjectMapper to simply deserialize to that collection type:
And if you added a toString() method to both those classes, printing collection would look something like this:
 
sushobhan bhaumik
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ron McLeod wrote:

sushobhan bhaumik wrote:after adding that dependency i am getting same issue ...


Please post your POM and code for the class where you are using ObjectMapper.



Hi now after executing getting below error.


Not able to upload file so pasting here

POM file:



java file:

Staff note (Ron McLeod):

Please use code tags when posting code, XML structures, etc. I have added them for you this time.

 
sushobhan bhaumik
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

after adding below code i am able to get read the json.

collection = mapper.readValue(json, MyClassName.class);

then i am grouping by cargo price



now i am getting a map with list of sizes where price is matching, then how i can get the range?
 
sushobhan bhaumik
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now instead of static json i am passing json like this as a parameter.



what all changes i need to do then, please help me in this..almost most of the things are done.
 
And when my army is complete, I will rule the world! But, for now, I'm going to be happy with this tiny ad:
Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps
https://coderanch.com/t/722574/Sauce-Labs-World-Largest-Continuous
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!