Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Flask Basics Character Builder Create a JSON String

Create a function named to_json that takes a single argument. Convert the argument to string with the json library and r

is there alternative way for string that I am missing? please help =)

json_string.py
import json
apple = 10
def to_json():
  return str(apple)
  return apple

7 Answers

Raymond Wach
Raymond Wach
7,961 Points

As you've defined it, to_json currently takes no arguments. You need to declare a variable in the parameter list of your function; something like:

def foo(bar):
  return bar

The next step is to use the appropriate method from the json library to convert the argument to a JSON string. You will probably want to save that somehow or return the converted value.

Sean McKeown
Sean McKeown
23,267 Points

Well first you need to pass an argument to the to_json function. Then figure out what method that the json library provides that does the conversion for you.

Create a function named to_json that takes a single argument. Convert the argument to string with the json library and return it.

Bummer! to_json() takes 0 positional arguments but 1 was given

import json def to_json(): return json.stringify(old, new) return to_json

this worked out better, but did not pass. is there a different method to use?

yipee! =D the solution was simpler than first thought.

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

Hey I'm wondering....Why the two return statements?

One for conversion and one for relay =)

Craig Dennis
Craig Dennis
Treehouse Teacher

You can only really have one return statement. The first return will exit the function the following line will never be reached.