[Bug] flattenAsMap produces escaped field names for field names that contain dot
See original GitHub issueHere’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:
- Created 3 years ago
- Comments:19 (6 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
(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:
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.