diff --git a/flatten_json.py b/flatten_json.py
index <HASH>..<HASH> 100644
--- a/flatten_json.py
+++ b/flatten_json.py
@@ -109,8 +109,17 @@ def unflatten(flat_dict, separator='_'):
 
         dic[keys[-1]] = value
 
-    for item in flat_dict:
-        _unflatten(unflattened_dict, item.split(separator), flat_dict[item])
+    list_keys = sorted(flat_dict.keys())
+    for i, item in enumerate(list_keys):
+        if i != len(list_keys)-1:
+            if list_keys[i] not in list_keys[i + 1]:
+                _unflatten(unflattened_dict, item.split(separator), flat_dict[item])
+            else:
+                pass  # if key contained in next key, json will be invalid.
+        else:
+            #  last element
+            _unflatten(unflattened_dict, item.split(separator), flat_dict[item])
+
 
     return unflattened_dict
Fix unflatten for dataframe with duplicate columns

Adding fix for unflattening from pandas dataframe records. See here: <URL>