Fix #1418 (problem with JSON unflatten when using common prefixes)

This is a tentative fix for issue #1418 (Problem with JSON unflatten when using common prefixes):

Some sample code to reproduce the problem is included in the issue.