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}

Recommended Answers

All 2 Replies

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)

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}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.