'Nested f-strings in Python (Third nested f-string)

I need to convert json to given hcl structure. There is this code:

user_dicts = { 'users': [ {'name': 'test1', 'pass': 'password1', 'permissions': [ {'access': 'yes', 'role': 'admin'}, {'access': 'yes', 'role': 'user'} ] }, {'name': 'test2', 'pass': 'password2', 'permissions': [ {'access': 'yes', 'role': 'admin'} ] } ] }

double_q = '"'

result = f"""
users = [
    {''.join([
    f'''{{
        name        = {double_q}{ d['name'] }{double_q},
        pass        = {double_q}{ d['pass'] }{double_q},
        permissions = [
            {''.join([f"{{ access = {double_q}{ d['permissions'][index]['access'] }{double_q}, role = {double_q}{ d['permissions'][index]['role'] }{double_q} }}," for index in range(len(d['permissions'])) ])}
        ]
    }},
    ''' for d in user_dicts['users']
    ])}
]"""

print(result)

which returns the following line:

users = [
    {
        name        = "test1",
        pass        = "password1",
        permissions = [
            { access = "yes", role = "admin" },{ access = "yes", role = "user" },
        ]
    },
    {
        name        = "test2",
        pass        = "password2",
        permissions = [
            { access = "yes", role = "admin" },
        ]
    },
    
]

What needs to be done so that there is a new line between the dictionaries in permissions while maintaining the number of spaces? That is:

users = [
    {
        name        = "test1",
        pass        = "password1",
        permissions = [
            { access = "yes", role = "admin" },
            { access = "yes", role = "user" },
        ]
    },
    {
        name        = "test2",
        pass        = "password2",
        permissions = [
            { access = "yes", role = "admin" },
        ]
    },
    
]

I tried adding f-string to new_line variable:

user_dicts = { 'users': [ {'name': 'test1', 'pass': 'password1', 'permissions': [ {'access': 'yes', 'role': 'admin'}, {'access': 'yes', 'role': 'user'} ] }, {'name': 'test2', 'pass': 'password2', 'permissions': [ {'access': 'yes', 'role': 'admin'} ] } ] }

double_q = '"'
new_line = '\n'

result = f"""
users = [
    {''.join([
    f'''{{
        name        = {double_q}{ d['name'] }{double_q},
        pass        = {double_q}{ d['pass'] }{double_q},
        permissions = [
            {f'{new_line}'.join([f"{{ access = {double_q}{ d['permissions'][index]['access'] }{double_q}, role = {double_q}{ d['permissions'][index]['role'] }{double_q} }}," for index in range(len(d['permissions'])) ])}
        ]
    }},
    ''' for d in user_dicts['users']
    ])}
]"""

print(result)

But the next line becomes from the beginning of the line:

users = [
    {
        name        = "test1",
        pass        = "password1",
        permissions = [
            { access = "yes", role = "admin" },
{ access = "yes", role = "user" },
        ]
    },
    {
        name        = "test2",
        pass        = "password2",
        permissions = [
            { access = "yes", role = "admin" },
        ]
    },
    
]


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source