Technical Thursday – JSON outputs in Python

Outputs, output formatting…this is a quite interesting topic during your development activities. Every time when you create a function you want to provide reusable code with excellent outputs…but how can you achieve this?

Maybe you feel this is a simple question and the answer is also simple. Nevertheless you realize some weeks later this is a little bit complex area. I mean when you choose a result type or solution you have some direction you can choose.

  • True/False
  • Simple string
  • Nothing 🙁
  • Result code (0, 1, …)
  • JSON

 

My personal choice is JSON because easy to manage and you can provide several important information for re-usage via that . For example in Azure-Cli every time you receive JSON object which contains the most important information for current activity. Moreover a JSON object is easily managed by Python.

 

And now I show some useful example for JSON output management by Python.

0. Use JSON in python

JSON management is very simple in Python. You merely need to import JSON module.

# Import JSON module
import json

# Define JSON format string
mystring = '{"name": "Python", "version": "2.7"}'

# Convert string to JSON
myjson = json.loads(mystring)

# Use JSON data
print "{0}".format(myjson["name"])

 

1. Return from string

When you have a string in JSON format in your function and you would like to use it as a JSON object you need to convert the string to JSON.

# Define JSON format string
mystring = '{"name": "Python", "version": "2.7"}'    

# my Function
def myFunction(inputString):
    # Import JSON module
    import json

    # Try to convert to JSON
    try:
        # Convert string to JSON
        myjson = json.loads(inputString)
        
        # Return with result
        return myjson["name"]
    except:
        # Error handling
        return False


# Call function
myFunction(mystring)

To convert string to JSON object you can use the “json.loads()” method.

 

2. Return from JSON

Sometimes you have a JSON object and you would like to give back it via your function in string format.

# Define JSON format string
mystring = '{"name": "Python", "version": "2.7"}'    

# my Function
def myFunction(inputString):
    # Import JSON module
    import json

    # Try to convert to JSON
    try:
        # Convert string to JSON
        myjson = json.loads(inputString)
        
        # Return with result
        return json.dumps(myjson)
    except:
        # Error handling
        return False


# Call function
myFunction(mystring)

To convert JSON object to string you can use the “json.dumps()” method.

 

3. Return custom string

Finally you can create string output which is built by you.

# my Function
def myFunction(inputString):
     # Return with result
     return '{"status": "success", "message": "%s"}' % (inputString) 

# Call function
myFunction("This is a string")

Here you should not import json module to create JSON format output

 

Of course can combine them and use other formats…Let’s try them…

Be the first to comment on "Technical Thursday – JSON outputs in Python"

Leave a comment

Positive SSL