JSON Conversion Function

  • Share this:

Code introduction


This function can accept a dictionary or a JSON formatted string as input. If the input is a dictionary, it will convert it to a JSON formatted string and return it; if the input is a string, it will parse it into a dictionary and return it.


Technology Stack : json

Code Type : Function

Code Difficulty : Intermediate


                
                    
def ajson(arg1, arg2):
    import json
    if isinstance(arg1, dict):
        return json.dumps(arg1, indent=4)
    elif isinstance(arg2, str):
        return json.loads(arg2)
    else:
        return None                
              
Tags: