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

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

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

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Java UDFs and Stored Procedures for Data Engineers: A Hands-On Guide
  • Using Java Class Extension Library for Data-Oriented Programming - Part 2
  • Using Java Class Extension Library for Data-Oriented Programming
  • Practical Generators in Go 1.23 for Database Pagination

Trending

  • Cookies Revisited: A Networking Solution for Third-Party Cookies
  • Blue Skies Ahead: An AI Case Study on LLM Use for a Graph Theory Related Application
  • Concourse CI/CD Pipeline: Webhook Triggers
  • Artificial Intelligence, Real Consequences: Balancing Good vs Evil AI [Infographic]
  1. DZone
  2. Data Engineering
  3. Data
  4. Accessing Nested Data Structures in Java

Accessing Nested Data Structures in Java

Four ways to quickly get at the data you need.

By 
Greg Brown user avatar
Greg Brown
·
Mar. 27, 21 · Tutorial
Likes (6)
Comment
Save
Tweet
Share
12.8K Views

Join the DZone community and get the full member experience.

Join For Free

Enterprise data structures are not usually flat. More often they are hierarchical, nesting one or more levels deep. For example, an account object might contain a customer object, which might contain an address object, and so on.

Such data structures are often returned by web services. A single call to the server requires less code and incurs less network overhead than multiple calls to retrieve the same information. Often, these data structures are returned as JSON and mapped to strongly typed equivalents such as Java beans on the client side. This works well when modeling the complete server response; however, it can be overkill when only a small subset of the returned data is required.

For example, the sample user service at typicode.com returns a collection of records structured like this:

JSON
 




xxxxxxxxxx
1
23


 
1
{
2
  "id": 1,
3
  "name": "Leanne Graham",
4
  "username": "Bret",
5
  "email": "Sincere@april.biz",
6
  "address": {
7
    "street": "Kulas Light",
8
    "suite": "Apt. 556",
9
    "city": "Gwenborough",
10
    "zipcode": "92998-3874",
11
    "geo": {
12
      "lat": "-37.3159",
13
      "lng": "81.1496"
14
    }
15
  },
16
  "phone": "1-770-736-8031 x56442",
17
  "website": "hildegard.org",
18
  "company": {
19
    "name": "Romaguera-Crona",
20
    "catchPhrase": "Multi-layered client-server neural-net",
21
    "bs": "harness real-time e-markets"
22
  }
23
} 



It would be straightforward to map this content to a bean representation. However, if the caller is only interested in the "catchPhrase" values, for example, it would still require the following class definitions at a minimum:

Java
 




xxxxxxxxxx
1
23


 
1
public static class User {
2
    private Company company;
3

          
4
    public Company getCompany() {
5
        return company;
6
    }
7

          
8
    public void setCompany(Company company) {
9
        this.company = company;
10
    }
11
}
12

          
13
public static class Company {
14
    private String catchPhrase;
15

          
16
    public String getCatchPhrase() {
17
        return catchPhrase;
18
    }
19

          
20
    public void setCatchPhrase(String catchPhrase) {
21
        this.catchPhrase = catchPhrase;
22
    }
23
}



Using HTTP-RPC‘s WebServiceProxy class, the following code could then be used to bind the response data to bean instances, extract the catch-phrase values, and convert them to a JSON list:

Java
 




xxxxxxxxxx
1
11


 
1
URL url = new URL("https://jsonplaceholder.typicode.com/users");
2

          
3
List<User> users = WebServiceProxy.get(url).invoke(BeanAdapter.typeOf(List.class, User.class));
4

          
5
List<String> catchPhrases = users.stream()
6
    .map(user -> user.getCompany().getCatchPhrase())
7
    .collect(Collectors.toList());
8

          
9
JSONEncoder jsonEncoder = new JSONEncoder();
10

          
11
jsonEncoder.write(catchPhrases, System.out);



For example:

JSON
 




xxxxxxxxxx
1
12


 
1
[
2
  "Multi-layered client-server neural-net",
3
  "Proactive didactic contingency",
4
  "Face to face bifurcated interface",
5
  "Multi-tiered zero tolerance productivity",
6
  "User-centric fault-tolerant solution",
7
  "Synchronised bottom-line interface",
8
  "Configurable multimedia task-force",
9
  "Implemented secondary concept",
10
  "Switchable contextually-based project",
11
  "Centralized empowering task-force"
12
]



Alternatively, interfaces could be used in place of the bean types to eliminate some of the boilerplate code:

Java
 




xxxxxxxxxx
1


 
1
public interface User {
2
    Company getCompany();
3
}
4

          
5
public interface Company {
6
    String getCatchPhrase();
7
}



The code for extracting the catch-phrases would be identical to the previous example, and the resulting output would be the same:

Java
 




xxxxxxxxxx
1
11


 
1
URL url = new URL("https://jsonplaceholder.typicode.com/users");
2

          
3
List<User> users = WebServiceProxy.get(url).invoke(BeanAdapter.typeOf(List.class, User.class));
4

          
5
List<String> catchPhrases = users.stream()
6
    .map(user -> user.getCompany().getCatchPhrase())
7
    .collect(Collectors.toList());
8

          
9
JSONEncoder jsonEncoder = new JSONEncoder();
10

          
11
jsonEncoder.write(catchPhrases, System.out);



A third option would be to deserialize the "raw" JSON data and access the catch-phrases via successive calls to Map#get():

Java
 




xxxxxxxxxx
1
11


 
1
URL url = new URL("https://jsonplaceholder.typicode.com/users");
2

          
3
List<Map<String, Map<String, ?>>> users = WebServiceProxy.get(url).invoke();
4

          
5
List<String> catchPhrases = users.stream()
6
    .map(user -> (String)user.get("company").get("catchPhrase"))
7
    .collect(Collectors.toList());
8

          
9
JSONEncoder jsonEncoder = new JSONEncoder();
10

          
11
jsonEncoder.write(catchPhrases, System.out);



This uses less code than the bean or interface approaches, but still requires the declaration of a moderately complex generic, even for this fairly simple case.

A fourth alternative would be to use the valueAt() method of HTTP-RPC’s Collectionsclass to access the nested values by key path:

Java
 




x
11


 
1
URL url = new URL("https://jsonplaceholder.typicode.com/users");
2

          
3
List<?> users = WebServiceProxy.get(url).invoke();
4

          
5
List<String> catchPhrases = users.stream()
6
    .map(user -> (String)Collections.valueAt(user, "company", "catchPhrase"))
7
    .collect(Collectors.toList());
8

          
9
JSONEncoder jsonEncoder = new JSONEncoder();
10

          
11
jsonEncoder.write(catchPhrases, System.out);



This approach is the least verbose, as it allows the caller to retrieve the desired data directly, without the need for intermediate types or nested generics.

If a caller needs access to most or all of the data returned by a service, then binding to bean or interface types is probably the most practical solution. However, if access to only a targeted subset of nested data is required (e.g. for lightweight transformation or basic validation), then the generic map or valueOf() approach may be preferable.

For more information, see the project README.

Data (computing) Java (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • Java UDFs and Stored Procedures for Data Engineers: A Hands-On Guide
  • Using Java Class Extension Library for Data-Oriented Programming - Part 2
  • Using Java Class Extension Library for Data-Oriented Programming
  • Practical Generators in Go 1.23 for Database Pagination

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!