Sign Up

Have an account? Sign In Now

Sign In

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have a permission to ask a question, You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here
Sign InSign Up

ErrorCorner

ErrorCorner Logo ErrorCorner Logo

ErrorCorner Navigation

  • Home
  • Contact Us
  • About Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Contact Us
  • About Us
Home/ Questions/Q 556
Next
Answered
Kenil Vasani
Kenil Vasani

Kenil Vasani

  • 646 Questions
  • 567 Answers
  • 77 Best Answers
  • 26 Points
View Profile
  • 9
Kenil Vasani
Asked: December 14, 20202020-12-14T21:04:53+00:00 2020-12-14T21:04:53+00:00In: Python

TypeError: the JSON object must be str, not ‘dict’

  • 9

Sorry guys, I couldn’t find the satisfying answer to print part of json response. Can someone help me here please:

import json
import requests
import pprint 

response = requests.get('<api endpoing>')
json_data = response.json()
print(json.dumps(json_data, indent=4, sort_keys=True))

Json response would be

{
    "Value1": "SomeValue",
    "data": {
        "subval1": false,
        "subval2": "0a4",
        "subval3": "",
        "subval4": "Click h!",
        "subval5": "1002",
        "subval6": "932",
        "subval7": "i2",
        "subval8": 250,
        "subval9": 0,
        "subval10": 1,
        "subval11": 3,
        "subval12": 1,
        "subval13": "<!>",
        "subval14": "",
        "subval15": "Click !!",
        "subval16": "",
        "subval17": 300
    },
    "error": true,
    "message": "Success",
    "status": 200
}

Now, I would like to traverse and print only the “data”: values. I will do the following

data = json.loads(json_data)
data_set = (data['data'])
print(data_set)

But the error Im getting: TypeError: the JSON object must be str, not ‘dict’

jsonpythonpython-3.x
  • 1 1 Answer
  • 10 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook

    1 Answer

    • Voted
    1. Kenil Vasani

      Kenil Vasani

      • 646 Questions
      • 567 Answers
      • 77 Best Answers
      • 26 Points
      View Profile
      Best Answer
      Kenil Vasani
      2020-12-14T21:02:23+00:00Added an answer on December 14, 2020 at 9:02 pm

      You don’t need to json.loads(json_data) as it is already a python dict, you just need to output this dict directly. And outputing json string from a dict is json.dumps()‘s job :

      json.dumps(json_data["data"])
      
      • 5
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp

    You must login to add an answer.

    Forgot Password?

    Sidebar

    Ask A Question
    • Popular
    • Kenil Vasani

      SyntaxError: invalid syntax to repo init in the AOSP code

      • 5 Answers
    • Kenil Vasani

      Homebrew fails on MacOS Big Sur

      • 3 Answers
    • Kenil Vasani

      runtimeError: package fails to pass a sanity check for numpy ...

      • 3 Answers
    • Kenil Vasani

      xlrd.biffh.XLRDError: Excel xlsx file; not supported

      • 3 Answers
    • Kenil Vasani

      Error: Node Sass version 5.0.0 is incompatible with ^4.0.0

      • 2 Answers

    Explore

    • Most Answered
    • Most Visited
    • Most Voted
    • Random

    © 2020-2021 ErrorCorner. All Rights Reserved
    by ErrorCorner.com