question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Bug] flattenAsMap produces escaped field names for field names that contain dot

See original GitHub issue

Here’s a code sample to demonstrate the issue.

public static void main(String[] args) throws Exception {
	final ObjectMapper mapper = new ObjectMapper();
	/*
	 * The input is just a simple json object { "a.b": 1 }
	 */
	final String jsonString = "{ \"a.b\": 1 }";
	final String flattenedString = JsonFlattener.flatten(jsonString);
	/*
	 * The following line prints
	 * {"[\"a.b\"]":1}
	 *
	 * No problem here.
	 */
	System.out.println(flattenedString);
	/*
	 * Print the only field name in the flattened json. It prints
	 * ["a.b"]
	 *
	 * No problem here either.
	 */
	System.out.println(mapper.readTree(flattenedString).fieldNames().next());
	final Map<String, Object> flattenedMap = JsonFlattener.flattenAsMap(jsonString);
	/*
	 * Print the only field name in the flattened map. It prints
	 * [\"a.b\"]
	 *
	 * And here's the problem. For some reason the quotes in the field name in the
	 * flattened map got unnecessarily escaped.
	 */
	System.out.println(flattenedMap.keySet().iterator().next());
	/*
	 * Just to confirm this, I'm parsing the map into json and printing the json.
	 * It prints
	 * {"[\\\"a.b\\\"]":1}
	 *
	 * So yes, the flattened map field name got unnecessarily escaped
	 */
	System.out.println(mapper.writeValueAsString(mapper.valueToTree(flattenedMap)));
}

So the gist of the issue is that, when you have a field name with a dot, the flatten method flattens things correctly, but flattenAsMap produces a Map with field names that got unnecessarily (and incorrectly) escaped.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:19 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
slisaasquatchcommented, Mar 13, 2021

(Outdated)

@jpomykala I think you must be doing something wrong with your JSON input. Here’s my code sample and it does give me the correct result:

public static void main(String[] args) {
	final String jsonStr = "{\n" +
			"                    \"login\": {\n" +
			"                        \"page\": {\n" +
			"                            \"name\": {\n" +
			"                                \"first\": \"first name\",\n" +
			"                                \"last\": \"last name\"\n" +
			"                            },\n" +
			"                            \"address\" : {\n" +
			"                                \"line_1\": \"Login page address line_1\",\n" +
			"                                \"line_2\": \"Login page address line_2\"\n" +
			"                            }\n" +
			"                        }\n" +
			"                    }\n" +
			"                }";
	final Map<String, Object> m = new JsonFlattener(jsonStr).withSeparator('.').flattenAsMap();
	System.out.println(m);
	// prints {"login.page.name.first":"first name","login.page.name.last":"last name","login.page.address.line_1":"Login page address line_1","login.page.address.line_2":"Login page address line_2"}
	System.out.println(m.keySet());
	// prints [login.page.name.first, login.page.name.last, login.page.address.line_1, login.page.address.line_2]
}
1reaction
wnamelesscommented, Oct 24, 2020

Hi @zubair-farooqui

Dot(.) is a reserved word which is used to be separators in keys of the flattened JSON. If you wan to change the default separator setting, you simply apply #withSeparator(‘*’) (you can change * to any character you want to use) to either JsonFlattener or JsonUnflattener, in doing so, your problem should be solved.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Support for dots in field names - IBM
Here the rules of using field names with dots in them with : You can insert a document that has a field name...
Read more >
Allowing elasticsearch to insert dots in field names
I am trying to insert data in elasticsearch (ver 2.3) which contains dot in the field names. The field names are pretty dynamic...
Read more >
Flexjson / Bugs / #25 escaping for dots in key names
I was attempting to exclude a json field that contained "." (dot) in the name of the field, and apparently this cannot be...
Read more >
Is it okay to put dots in SQL Server database names?
However choosing a database that contains a dot will produce an error ... all databases and doesn't escape the database names propperly.
Read more >
Is it bad practice for folder name to contain a dot ... - Ask Ubuntu
5 , makes the file or folder hidden. You can toggle the display of hidden files by pressing Ctrl + H in Nautilus....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found