Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

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

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

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

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8059675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T09:44:53+00:00 2026-06-05T09:44:53+00:00

I’m using ast.literal_eval to change the data I receive from json.loads() into a Python

  • 0

I’m using ast.literal_eval to change the data I receive from json.loads() into a Python dictionary; however if I should just be going about this an entirely different way – feel free to point that out as well.

# Authentication
buf = StringIO.StringIO()
c = pycurl.Curl()
c.setopt(c.URL, "https://kippt.com/api/account")
c.setopt(c.WRITEFUNCTION, buf.write)
c.setopt(c.HTTPHEADER, header)
c.perform()

result = buf.getvalue()
buf.close()

print result

# Printing Output
data_string = json.dumps(result)
jsonload = json.loads(data_string)
jsondict = ast.literal_eval(jsonload)

Currently it works properly with a one line JSON return, eg:

{“username”: “my_username”, “api_token”: “my_api_token”}

and I can get the values properly with:

print jsondict['username']
print jsondict['api_token']

The part where I’m having an issue with is when the data is nested, such as:

{“meta”: {“next”: null, “total_count”: 6, “previous”: null, “limit”:
20, “offset”: 0}, “objects”: [{“rss_url”:
“https://kippt.com/feed/username_here/stuff_here/cool-stuff”,
“updated”: “1339003710”, “title”: “Cool Stuff”, “created”:
“1339001514”, “slug”: “cool-stuff”, “id”: 54533, “resource_uri”:
“/api/lists/54533/”}, {“rss_url”:
“https://kippt.com/feed/username_here/stuff_here/programming”,
“updated”: “1339003479”, “title”: “Programming”, “created”:
“1339001487”, “slug”: “programming”, “id”: 54532, “resource_uri”:
“/api/lists/54532/”}, {“rss_url”:
“https://kippt.com/feed/username_here/stuff_here/android”,
“updated”: “1339003520”, “title”: “Android”, “created”: “1339000936”,
“slug”: “android”, “id”: 54530, “resource_uri”: “/api/lists/54530/”},
{“rss_url”:
“https://kippt.com/feed/username_here/stuff_here/chrome”,
“updated”: “1339000931”, “title”: “Chrome”, “created”: “1339000412”,
“slug”: “chrome”, “id”: 54529, “resource_uri”: “/api/lists/54529/”},
{“rss_url”:
“https://kippt.com/feed/username_here/stuff_here/inbox”,
“updated”: “1338946730”, “title”: “Inbox”, “created”: “1338945940”,
“slug”: “inbox”, “id”: 54432, “resource_uri”: “/api/lists/54432/”},
{“rss_url”:
“https://kippt.com/feed/username_here/stuff_here/read-later”,
“updated”: “1338945940”, “title”: “Read Later”, “created”:
“1338945940”, “slug”: “read-later”, “id”: 54433, “resource_uri”:
“/api/lists/54433/”}]}

When I use the same code (Exchange URL for /api/lists) then I get the following error upon running the script:

Traceback (most recent call last): File “kippt.py”, line 48, in

jsondict = ast.literal_eval(jsonload) File “/usr/local/lib/python2.7/ast.py”, line 80, in literal_eval
return _convert(node_or_string) File “/usr/local/lib/python2.7/ast.py”, line 63, in _convert
in zip(node.keys, node.values)) File “/usr/local/lib/python2.7/ast.py”, line 62, in
return dict((_convert(k), _convert(v)) for k, v File “/usr/local/lib/python2.7/ast.py”, line 63, in _convert
in zip(node.keys, node.values)) File “/usr/local/lib/python2.7/ast.py”, line 62, in
return dict((_convert(k), _convert(v)) for k, v File “/usr/local/lib/python2.7/ast.py”, line 79, in _convert
raise ValueError(‘malformed string’) ValueError: malformed string

Any help would be appreciated. Thanks!

Edit – Answer below:

Looks like my first input could have been interpretted as Python syntax, which is where my fault lay as I technically wasn’t doing it the right way to begin with even with that.

I now just want to json.loads() my result from cURL rather than doing the screwy stuff I was doing before.

eg:

buf = StringIO.StringIO()
c = pycurl.Curl()
c.setopt(c.URL, "https://kippt.com/api/lists")
c.setopt(c.WRITEFUNCTION, buf.write)
c.setopt(c.HTTPHEADER, header)
c.perform()

result = buf.getvalue()
buf.close()

print result

# Printing Output
jsonload = json.loads(result)
print jsonload['meta']['total_count'] # Gets the total_count item in the meta object.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T09:44:54+00:00Added an answer on June 5, 2026 at 9:44 am

    ast.literal_eval has no problem with nested dictionaries:

    >>> ast.literal_eval("{'a': {'b':'c'}}")
    {'a': {'b': 'c'}}
    

    ast.literal_eval is breaking because the data are, in fact, JSON… And JSON is not valid Python. Specifically, null is not a valid Python literal.

    Why not just use json.loads() to load the data?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am currently running into a problem where an element is coming back from
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.