Skip to content

JSON pointer query, trailing slashes and empty keys #410

@mfriedemann

Description

@mfriedemann

Hey,

I found an inconsistency, or potentially a bug, with the handling of JSON pointers in JSONObject.query().

Specifically, the handling of trailing slashes differs between the root element and elements further down.

From reading the RFC and a discussion about it, I got the impression that a trailing slash is supposed to refer to an empty key, but this only works with the root element.
For elements further down, a trailing slash will not select the empty key below the element, but the element itself instead.

example code

public void testPointerQuery() {
	JSONObject none = new JSONObject();
	JSONObject emptyKeyRoot = new JSONObject("{'':'empty key at root'}");
	JSONObject emptyKeyBelow = new JSONObject("{'foo':{'':'empty key below foo'}}");
	
	System.out.println("json=" + none + " ptr='' query -> " + none.query(""));
	System.out.println("json=" + none + " ptr='/' query -> " + none.query("/"));
	
	System.out.println("json=" + emptyKeyRoot + " ptr='' query -> " + emptyKeyRoot.query(""));
	System.out.println("json=" + emptyKeyRoot + " ptr='/' query -> " + emptyKeyRoot.query("/"));
	
	System.out.println("json=" + emptyKeyBelow + " ptr='' query -> " + emptyKeyBelow.query(""));
	System.out.println("json=" + emptyKeyBelow + " ptr='/' query -> " + emptyKeyBelow.query("/"));
	System.out.println("json=" + emptyKeyBelow + " ptr='/foo' query -> " + emptyKeyBelow.query("/foo"));
	System.out.println("json=" + emptyKeyBelow + " ptr='/foo/' query -> " + emptyKeyBelow.query("/foo/"));
}

output for example code above

json={} ptr='' query -> {}
json={} ptr='/' query -> null
json={"":"empty key at root"} ptr='' query -> {"":"empty key at root"}
json={"":"empty key at root"} ptr='/' query -> empty key at root
json={"foo":{"":"empty key below foo"}} ptr='' query -> {"foo":{"":"empty key below foo"}}
json={"foo":{"":"empty key below foo"}} ptr='/' query -> null
json={"foo":{"":"empty key below foo"}} ptr='/foo' query -> {"":"empty key below foo"}
json={"foo":{"":"empty key below foo"}} ptr='/foo/' query -> {"":"empty key below foo"}

expected output (last line)

json={"foo":{"":"empty key below foo"}} ptr='/foo/' query -> empty key below foo

This happens with both the string and URI fragment representations, using the 20180130 maven artifact.

Thanks,
M.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions