You can make your if statement more sophisticated by adding an else statement when needed.
Next 2 examples will clarify the implementation of this idea for you:
Tips
1- Requests library also has a json() method which can be confused with json library and it json.loads() and json.dumps() methods. Requests library’s json() method is a nice touch that can be used to convert data to json format inside the requests library.
Advanced Concepts (Optional)
1- You can use indentation with json.dumps() method so that the output will appear more beautiful and structured. Just add the parameter: indent=3 to inside the json.dumps() paranthesis.
Example 2
>>> data = [{“word”:”etiquette”,”score”:1647,”numSyllables”:3},{“word”:”aesthetic it”,”numSyllables”:4},{“word”:”arithmetic it”,”numSyllables”:5},{“word”:”geomagnetic cut”,”numSyllables”:6},{“word”:”pathetic it”,”numSyllables”:4}]
>>> import json >>> data = json.dumps(data, indent=4): >>> print(data)
Thank you for visiting our website! We use essential cookies to help us understand and enhance user experience. By clicking “Accept”, you consent to the use of ALL the cookies.