pythonjsonreaderjsonreader

Json entry Login reader


I made a Json reader in python that reads data from a file named data.json but i don't know how to read an entry named Login from entry named Amazon

{
    "Facebook": [
        {
            "Login": "John",
            "Password": "mycatisgreat"
        }
    ],
    "Twitch": [
        {
            "Login": "matelek",
            "Password": "password"
        }
    ],
    "Xkom": [
        {
            "Login": "neural",
            "Password": "neuralpass"
        }
    ],
    "Amazon": [
        {
            "Login": "john",
            "Password": "amazonisgreat777"
        }
    ]
}

this is the data.json file

import json


def json_read():
    with open("data.json", "r") as f:
        data = json.load(f)
        Amazon_content = data.get("Amazon")

        if Amazon_content is not None:

            Amazon_join = ", ".join(str(item) for item in Amazon_content)
            Amazon_strip = Amazon_join.replace("{", "")
            Amazon_strip_2 = Amazon_strip.replace("'", "")
            Amazon_strip_3 = Amazon_strip_2.replace("}", "")
            print(f"Amazon:\n    {Amazon_strip_3}")

        with open("json_output.txt", "w") as file:
            file.write(
                f"Amazon:\n    {Amazon_strip_3}"
            )


json_read()

Thanks.

I tried Amazon_content = data.get("Amazon", "Login") but it didn't work I was expecting some type of data but I did not get any


Solution

  •   import json
    
    def json_read():
        with open("data.json", "r") as f:
            data = json.load(f)
            Amazon_content = data.get("Amazon")
    
            if Amazon_content is not None:
                # Assuming there is only one entry in the "Amazon" section
                amazon_entry = Amazon_content[0]
                amazon_login = amazon_entry.get("Login")
    
                if amazon_login is not None:
                    print(f"Amazon Login: {amazon_login}")
    
                    with open("json_output.txt", "w") as file:
                        file.write(f"Amazon Login: {amazon_login}")
    
    json_read()
    

    try using this code!