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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Jakarta EE 12: Entering the Data Age of Enterprise Java
  • Building Realistic Test Data in Java: A Hands-On Guide for Developers
  • Java UDFs and Stored Procedures for Data Engineers: A Hands-On Guide
  • Using Java Class Extension Library for Data-Oriented Programming - Part 2

Trending

  • New York City Taxi Trips: Putting Neo4j and DuckDB to Work
  • When a Coding Agent Fixes the Test Instead of the Bug
  • Member Spotlight: Shai Almong
  • Architecture Review Boards for Generative AI: A Framework for Governance, Risk Management, and Responsible Adoption

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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
·
Mar. 27, 21 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
13.5K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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": "[email protected]",
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

  • Jakarta EE 12: Entering the Data Age of Enterprise Java
  • Building Realistic Test Data in Java: A Hands-On Guide for Developers
  • Java UDFs and Stored Procedures for Data Engineers: A Hands-On Guide
  • Using Java Class Extension Library for Data-Oriented Programming - Part 2

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

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 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook