'Get Folders name into JSON specific format

Trying to get folders name (without file names) from a directory and covert them into JSON format. So after 4 iteration of the folders, i will try to get userinput from the user. My current working code is:

import glob
import json
import io

in0  = input("Enter Userinput0:\n")
in1  = input("Enter Userinput1:\n") 
in2  = input("Enter Userinput2:\n")
in3  = input("Enter Userinput3:\n")
name = "somename"

def path_to_dict(path):

data = {os.path.basename(path): {}}
if os.path.isdir(path):
count = 0
    for x in os.listdir(path):
        data[os.path.basename(path)] =[path_to_dict(os.path.join(path,x))]
    count += 1
    if count == 4:
        break
    return data
path_to_dict('some path')
d = {"Userinput0": in0}
d[name] = {data: {"Userinput1": in1,"Userinput2": in2,"Userinput3": in3, }}
j = json.dumps(d, indent=5)
print (j)

Output for the above code:

TypeError: unhashable type: 'dict'

My tree structure:

|-Folder1
|--Subfolder       
|---Subfolder
|----Subfolder 
|-----Subfolder

Output JSON format i am trying to get:

{
  "Userinput0": "x.x", 
  "Somename": {
    "Folder1": {
      "subfolder": {
         "subfolder": {
            "subfolder": {
                "subfolder": {
                    "Userinput1": "x",
                    "Userinput2": "Y",
                    "Userinput3": Z
                }
            }
        }
     }
   }
}

I have edited my question and current code. Still i am not able to attain desired output So could anyone please help me to achieve the output.



Solution 1:[1]

def path_to_dict(path):

data = {os.path.basename(path): {
                "Userinput1": in1,
                "Userinput2": in2,
                "Userinput3": in3,
            }}
if os.path.isdir(path):
    count = 0
    for x in os.listdir(path):
        data[os.path.basename(path)] =path_to_dict(os.path.join(path,x))
        count += 1
        if count == 4:
            break
    
    return data

print(path_to_dict)

Sources

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

Source: Stack Overflow

Solution Source
Solution 1 Hanterzoone007