Skip to content

Add NestedJsonPathWrapper class for nested JSON access #1045

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

BsoBird
Copy link

@BsoBird BsoBird commented Jul 3, 2025

NestedJsonPathWrapper Class Documentation

Overview

JSON path wrapper that supports accessing nested JSON strings using delimiter syntax.

Extends DocumentContext functionality to handle JSON strings embedded within JSON objects. Uses special delimiter syntax {field} to access nested JSON content.

Key Features

  • Nested JSON access with delimiter syntax
  • Lazy loading for performance optimization
  • Configurable delimiters to avoid conflicts
  • Thread-safe operations
  • Automatic caching of parsed documents

Usage Examples

Basic Usage

// Data example: {"user": {"name": "Alice"}}
Map<String, Object> jsonData = new HashMap<>();
NestedJsonPathWrapper wrapper = NestedJsonPathWrapper.wrapJsonOrMap(jsonData);
wrapper.putValueByPath("$.user.name", "Alice");
Object name = wrapper.getValueByPath("$.user.name");        // Gets "Alice"

Nested JSON Access with Default Delimiters

Map<String, Object> nestedData = new HashMap<>();
nestedData.put("profile", "{\"name\":\"John\",\"age\":30,\"address\":{\"city\":\"NYC\",\"zip\":\"10001\"}}");
nestedData.put("items", "[{\"id\":1,\"name\":\"item1\"},{\"id\":2,\"name\":\"item2\"}]");
nestedData.put("tags", "[\"java\",\"json\",\"api\"]");

NestedJsonPathWrapper nestedWrapper = NestedJsonPathWrapper.wrapJsonOrMap(nestedData);

// Nested object access
String name = (String) nestedWrapper.getValueByPath("$.{profile}.name");           // Gets "John"
String city = (String) nestedWrapper.getValueByPath("$.{profile}.address.city");  // Gets "NYC"
nestedWrapper.putValueByPath("$.{profile}.age", 35);                              // Update age

// Nested array access
String itemName = (String) nestedWrapper.getValueByPath("$.{items}[0].name");     // Gets "item1"
String tag = (String) nestedWrapper.getValueByPath("$.{tags}[1]");                // Gets "json"
nestedWrapper.putValueByPath("$.{items}[0].name", "newItem1");                    // Update item name

// JSONPath functions on nested data
Integer itemCount = (Integer) nestedWrapper.getValueByPath("$.{items}.length()"); // Gets array length
Object allIds = nestedWrapper.getValueByPath("$.{items}[*].id");                  // Gets all IDs

Custom Delimiters

When default {} conflicts with JSON keys:

// Data example: {"profile": "{\"name\":\"John\"}", "{profile}": "{\"name\":\"Jane\"}"}
Map<String, Object> data = new HashMap<>();
data.put("profile", "{\"name\":\"John\"}");        // Normal key
data.put("{profile}", "{\"name\":\"Jane\"}");      // Key contains {}

NestedJsonPathWrapper customWrapper = NestedJsonPathWrapper.wrapJsonOrMap(data, true, "«", "»");
Object value1 = customWrapper.getValueByPath("$.«profile».name");      // Gets "John"
Object value2 = customWrapper.getValueByPath("$.«{profile}».name");    // Gets "Jane"

@BsoBird BsoBird changed the title support NestedJsonPath process Support NestedJsonPath process Jul 3, 2025
@BsoBird BsoBird changed the title Support NestedJsonPath process Add NestedJsonPathWrapper class for nested JSON access Jul 3, 2025
@BsoBird
Copy link
Author

BsoBird commented Jul 3, 2025

@kallestenflo @jochenberger @uarlouski
hello sir, can u check this? tks!

@BsoBird
Copy link
Author

BsoBird commented Jul 3, 2025

@jochenberger Sir, if you think this PR is not appropriate, could you tell me the reason?

@BsoBird BsoBird closed this Jul 3, 2025
@BsoBird
Copy link
Author

BsoBird commented Jul 3, 2025

@jochenberger Sir, I know this doesn't belong to the jsonPath specification, but in reality, we often encounter scenarios where JSON strings are nested within each other. Compared to writing lengthy code to parse nested JSON, isn't it much more convenient to use a single jsonPath for assignment or value retrieval? We've extensively used it in production environments and received positive feedback. For users, it's genuinely useful and solves real problems.
If you could tell me the reasons you dislike this PR, that would be great. I'm willing to listen to any critical suggestions.

@BsoBird BsoBird deleted the supportNestedJsonPath branch July 3, 2025 12:22
@jochenberger
Copy link
Contributor

I didn't look at the PR actually, I just down-voted your comment because I don't like comments that just mention people to get their attention.

@BsoBird
Copy link
Author

BsoBird commented Jul 3, 2025

I didn't look at the PR actually, I just down-voted your comment because I don't like comments that just mention people to get their attention.

I'm sorry, sir, I really shouldn't have notified so many people, but kallestenflo has left no trace of activity on GitHub, and I didn't know who could review the code, so I took a chance and notified the top three contributors on the list. If this has caused you dissatisfaction, I apologize for my actions.

@BsoBird BsoBird reopened this Jul 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants