Question

Rebuild a flattened json file?

  • 4 May 2023
  • 2 replies
  • 15 views

Userlevel 4
Badge +26

Has anyone got a method to rebuild a flattened json file using the dot notation?

 

Essentially it would be the JSONAttributePacker but with a iterative approach?


2 replies

Userlevel 3
Badge +16

I had never heard of dot notation before, so I took a look at the JSONAttributePacker, saw it works with Python. So I asked GPT4 to do this conversion with Python, and you're right it has a recursive approach. It's response is below:

 

You can convert a regular JSON file to dot notation using Python without any external library dependencies by implementing a recursive function. Here's a code snippet to do this:

import json
 
def json_to_dot_notation(json_obj, prefix='', result=None):
    if result is None:
        result = {}
        
    if isinstance(json_obj, dict):
        for key, value in json_obj.items():
            new_prefix = f"{prefix}.{key}" if prefix else key
            json_to_dot_notation(value, new_prefix, result)
    elif isinstance(json_obj, list):
        for index, value in enumerate(json_obj):
            new_prefix = f"{prefix}.{index}" if prefix else str(index)
            json_to_dot_notation(value, new_prefix, result)
    else:
        result[prefix] = json_obj
        
    return result
 
# Load JSON from file
with open('your_json_file.json', 'r') as file:
    json_data = json.load(file)
 
# Convert JSON to dot notation
dot_notation = json_to_dot_notation(json_data)
 
# Print result
for key, value in dot_notation.items():
    print(f"{key}: {value}")

 

Replace `'your_json_file.json'` with the path to your JSON file. The `json_to_dot_notation()` function takes a JSON object, iterates through it, and creates a new dictionary with the keys in dot notation format. The function handles nested dictionaries and lists.

 

Hopefully that helps

Userlevel 4
Badge +26

I had never heard of dot notation before, so I took a look at the JSONAttributePacker, saw it works with Python. So I asked GPT4 to do this conversion with Python, and you're right it has a recursive approach. It's response is below:

 

You can convert a regular JSON file to dot notation using Python without any external library dependencies by implementing a recursive function. Here's a code snippet to do this:

import json
 
def json_to_dot_notation(json_obj, prefix='', result=None):
    if result is None:
        result = {}
        
    if isinstance(json_obj, dict):
        for key, value in json_obj.items():
            new_prefix = f"{prefix}.{key}" if prefix else key
            json_to_dot_notation(value, new_prefix, result)
    elif isinstance(json_obj, list):
        for index, value in enumerate(json_obj):
            new_prefix = f"{prefix}.{index}" if prefix else str(index)
            json_to_dot_notation(value, new_prefix, result)
    else:
        result[prefix] = json_obj
        
    return result
 
# Load JSON from file
with open('your_json_file.json', 'r') as file:
    json_data = json.load(file)
 
# Convert JSON to dot notation
dot_notation = json_to_dot_notation(json_data)
 
# Print result
for key, value in dot_notation.items():
    print(f"{key}: {value}")

 

Replace `'your_json_file.json'` with the path to your JSON file. The `json_to_dot_notation()` function takes a JSON object, iterates through it, and creates a new dictionary with the keys in dot notation format. The function handles nested dictionaries and lists.

 

Hopefully that helps

Haha, awesome - not quite what I'm after. Perhaps my language was a bit sloppy. After you've used FME to recursively flattened a JSON snippet into attributes you end up with attributes like "itmes.name", "items.parameters.hieght" where each "." represents effectively a nested objects and "items.parameters.bbox{0}" might represent an item in an array.

 

So the json might look like 

 

{
    "items": "name",
    "parametres": {
        "height": 50,
        "bbox": [0, 0, 10, 10]
    }

But then if you want to put the flattened structure back into JSON it becomes a real pain. There are indeed ways to do it but it would be pretty nice if there was an easy and dynamic way to "unflatten" fme attributes with the "dot" structure back into a JSON feature.

 

I do think python is the way to go if there is nothing already out there. I was kind of hoping someone already had a solution 

Reply