Javatpoint Logo
Javatpoint Logo

Convert string to JSON in Python

Before taking a deep dive into the topic, let us have a glance on what strings are and what is JSON?

Strings: are a sequence of characters that are denoted using inverted commas ''. They are immutable which means they cannot be changed once declared.

JSON: stands for "JavaScript Object Notation", the JSON files consist of text that can be easily read by humans and is present in the form of attribute-value pairs.

The extension of JSON files is ".json"

Let us have a look at the first approach of converting a string to json in Python.

The following program illustrates the same.

Output:

The declared dictionary is {'C_code': 1, 'C++_code' : 26,
      'Java_code' : 17, 'Python_code' : 28}
It's type is <class 'str'>
The resultant dictionary is {'C_code': 1, 'C++_code' : 26,
      'Java_code' : 17, 'Python_code' : 28}
The type of resultant dictionary is <class 'dict'>

Explanation:

It's time to see the explanation so that our logic becomes clear-

  1. Since here the objective is to convert a string to json file we will first import the json module.
  2. The next step is to initialize the json object in which we have the subject name as keys and then their corresponding values are specified.
  3. After this, we have used dumps() to convert a Python object to a json string.
  4. Finally, we will use loads() to parse a JSON string and convert it into a dictionary.

Using eval()

Output:

The declared dictionary is   {'C_code': 1, 'C++_code' : 26,
			'Java_code' : 17, 'Python_code' : 28}

Its type is  <class 'str'>
The resultant dictionary is  {'C_code': 1, 'C++_code': 26, 'Java_code': 17, 'Python_code': 28}
The type of resultant dictionary is  <class 'dict'>

Explanation:

Let us understand what we have done in the above program.

  1. Since here the objective is to convert a string to json file we will first import the json module.
  2. The next step is to initialize the json object in which we have the subject name as keys and then their corresponding values are specified.
  3. After this, we have used eval() to convert a Python string to json.
  4. On executing the program, it displays the desired output.

Fetching values

Finally, in the last program we will fetch the values after the conversion of string to json.

Let's have a look at it.

Output:

1
17

We can observe the following things in the output-

  1. We have converted the string to json using json.loads().
  2. After this we have used the keys "C_code" & "Java_code" to fetch their corresponding values.

Conclusion

In this Tutorial, we learned how to convert a string to json using Python.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA