I am getting output as json string with the following code. But I require it as JSON dict without leading and trailing quotes. How can I do that?

It seems to work if I uncomment line4 but not with raw input.

def main(args):
    f = open(args[1], 'w')
    inp = raw_input("Enter a Dict")
    #    inp = {"a":1,"b":2}
    django.utils.simplejson.dumps(inp, f)

if __name__=="__main__":
    import sys
    main(sys.argv)

#Recieved O/P: "{'a':1,'b':2'}
#Required O/P: {'a':1,'b':2}

Perhaps convert to dict first

def main(args):
    f = open(args[1], 'w')
    inp = raw_input("Enter a Dict")
    #    inp = {"a":1,"b":2}
    inp = django.utils.simplejson.loads(inp)
    assert isinstance(inp, dict)
    django.utils.simplejson.dumps(inp, f)

Edited 3 Years Ago by Gribouillis

Maybe something along this line ...

dd = "{'a':1,'b':2}"

print(type(dd))  # <type 'str'>

d = eval(dd)

print(type(d))  # <type 'dict'>
print(d)        # {'a': 1, 'b': 2}
This question has already been answered. Start a new discussion instead.