Convert dict to json python

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    JSON stands for JavaScript Object Notation. It means that a script (executable) file which is made of text in a programming language, is used to store and transfer the data. Python supports JSON through a built-in package called json. To use this feature, we import the JSON package in Python script. The text in JSON is done through quoted-string which contains a value in key-value mapping within { }. It is similar to the dictionary in Python.
    Note: For more information, refer to Read, Write and Parse JSON using Python
     

    Function Used:  

    • json.dumps() 
       
    • json.dump() 
       

    Syntax: json.dumps(dict, indent)
    Parameters: 
     

    • dictionary – name of dictionary which should be converted to JSON object. 
       
    • indent – defines the number of units for indentation 
       

    Syntax: json.dump(dict, file_pointer)
    Parameters: 
     

    • dictionary – name of dictionary which should be converted to JSON object. 
       
    • file pointer – pointer of the file opened in write or append mode. 
       

    Example 1:

    Python3

    import json 

    dictionary =

      "id": "04"

      "name": "sunil"

      "department": "HR"

    json_object = json.dumps(dictionary, indent = 4

    print(json_object)

    Output

    {
        "id": "04",
        "name": "sunil",
        "department": "HR"
    }

    Output:

    {
        "department": "HR",
        "id": "04",
        "name": "sunil"
    }

    Example 2:

    Python3

    import json

    dictionary ={

        "name" : "sathiyajith",

        "rollno" : 56,

        "cgpa" : 8.6,

        "phonenumber" : "9976770500"

    }

    with open("sample.json", "w") as outfile:

        json.dump(dictionary, outfile)

    Output:
     

    Convert dict to json python


    View Discussion

    Improve Article

    Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    JSON stands for JavaScript Object Notation. It means that a script (executable) file which is made of text in a programming language, is used to store and transfer the data. Python supports JSON through a built-in package called json. To use this feature, we import the json package in Python script. The text in JSON is done through quoted-string which contains value in key-value mapping within { }. It is similar to the dictionary in Python.
    Function Used: 

    • json.load(): json.loads() function is present in python built-in ‘json’ module. This function is used to parse the JSON string.
       

    Syntax: json.load(file_name)
    Parameter: It takes JSON file as the parameter.
    Return type: It returns the python dictionary object. 
     

    Example 1: Let’s suppose the JSON file looks like this:
     

    Convert dict to json python

    We want to convert the content of this file to Python dictionary. Below is the implementation.
     

    Python3

    import json

    with open('data.json') as json_file:

        data = json.load(json_file)

        print("Type:", type(data))

        print("\nPeople1:", data['people1'])

        print("\nPeople2:", data['people2'])

    Output :
     

    Convert dict to json python

    Example 2: Reading nested data 
    In the above JSON file, there is a nested dictionary in the first key people1. Below is the implementation of reading nested data.
     

    Python3

    import json

    with open('data.json') as json_file:

        data = json.load(json_file)

        print(data['people1'][0])

        print("\nPrinting nested dictionary as a key-value pair\n")

        for i in data['people1']:

            print("Name:", i['name'])

            print("Website:", i['website'])

            print("From:", i['from'])

            print()

    Output :
     

    Convert dict to json python


    Can we convert dictionary to JSON in Python?

    Python possesses a default module, 'json,' with an in-built function named dumps() to convert the dictionary into a JSON object by importing the "json" module. "json" module makes it easy to parse the JSON strings which contain the JSON object.

    Can dictionary be converted to JSON?

    To Convert dictionary to JSON you can use the json. dumps() which converts a dictionary to str object, not a json(dict) object! so you have to load your str into a dict to use it by using json.

    How do you represent a dictionary in JSON?

    Introducing JSON JSON is a way of representing Arrays and Dictionaries of values ( String , Int , Float , Double ) as a text file. In a JSON file, Arrays are denoted by [ ] and dictionaries are denoted by { } .

    Is JSON and dict same in Python?

    json supports arrays via brackets. There is a difference between json and python dict. it also uses unhelpful language like "sucks" and makes subjective statements like "makes code look dirty" without backing up the claim. Yeah, this answer is conflating JSON (a format) with the way property access works in Javascript.