Flattening JSON objects in Python

JSON(JavaScript Object Notation) is a data-interchange format that is human-readable text and is used to transmit data, especially between web applications and servers. The JSON files will be like nested dictionaries in Python. To convert a text file into JSON, there is a json module in Python. This module comes in-built with Python standard modules, so there is no need to install it externally.

A flatten json is nothing but there is no nesting is present and only key-value pairs are present.

Example:



Unflattened JSON:
{‘user’ :{‘Rachel’:{‘UserID’:1717171717,
‘Email’: ‘rachel1999@gmail.com’,
‘friends’: [‘John’, ‘Jeremy’, ‘Emily’]}}}

Flattened JSON:
{‘user_Rachel_friends_2’: ‘Emily’, ‘user_Rachel_friends_0’: ‘John’, ‘user_Rachel_UserID’: 1717171717, ‘user_Rachel_Email’: ‘rachel1999@gmail.com’, ‘user_Rachel_friends_1’: ‘Jeremy’}

Need of flattening JSON

There are many reasons for the need of flattening JSON, such as for a better and understandable view that is there are only key-value pairs are present without any nesting. It also allows for context-specific security and constraints to be implemented in a readable, but in more verbose way.

Approach to flatten JSON

There are many ways to flatten JSON. There is one recursive way and another by using the json-flatten library.

  • Recursive Approach: Now we can flatten the dictionary array by a recursive approach which is quite easy to understand. The recursive approach is a bit slower than using json-flatten library.

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # for a array value of a key
    unflat_json = {'user' :
                   {'Rachel':
                    {'UserID':1717171717,
                    'Email': 'rachel1999@gmail.com'
                    'friends': ['John', 'Jeremy', 'Emily']
                    }
                   }
                  }
      
    # Function for flattening 
    # json
    def flatten_json(y):
        out = {}
      
        def flatten(x, name =''):
              
            # If the Nested key-value 
            # pair is of dict type
            if type(x) is dict:
                  
                for a in x:
                    flatten(x[a], name + a + '_')
                      
            # If the Nested key-value
            # pair is of list type
            elif type(x) is list:
                  
                i = 0
                  
                for a in x:                
                    flatten(a, name + str(i) + '_')
                    i += 1
            else:
                out[name[:-1]] = x
      
        flatten(y)
        return out
      
    # Driver code
    print(flatten_json(unflat_json))

    chevron_right

    
    

    Output :

    {‘user_Rachel_friends_2’: ‘Emily’, ‘user_Rachel_friends_0’: ‘John’, ‘user_Rachel_UserID’: 1717171717, ‘user_Rachel_Email’: ‘rachel1999@gmail.com’, ‘user_Rachel_friends_1’: ‘Jeremy’}

  • Using flatten_json library: json-flatten library provides functions for flattening a JSON object to a single key-value pairs, and unflattening that dictionary back to a JSON object.

    Installing library

    In order to use the flatten_json library, we need to install this library. flatten_json can be installed by running the following command in the terminal.

    pip install json-flatten

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    from flatten_json import flatten
      
    unflat_json = {'user' :
                   {'Rachel':
                    {'UserID':1717171717,
                    'Email': 'rachel1999@gmail.com'
                    'friends': ['John', 'Jeremy', 'Emily']
                    }
                   }
                  }
      
    flat_json = flatten(unflat_json)
      
    print(flat_json)

    chevron_right

    
    

    Output :

    {‘user_Rachel_UserID’: 1717171717, ‘user_Rachel_Email’: ‘rachel1999@gmail.com’, ‘user_Rachel_friends_0’: ‘John’, ‘user_Rachel_friends_1’: ‘Jeremy’, ‘user_Rachel_friends_2’: ‘Emily’}




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.