Search code examples
pythonjsonsimplejson

Replacing JSON value in python


EDIT: sorry, i had a hard to see uppercase/lowercase typo. please someone delete this question.

I am trying to change the value of a json object with simplejson. The problem is that instead of replacing it, it is adding another entry with the same key.

{
  "main" : "value_to_replace"
}

and after doing this in python:

json["main"] = "replaced"

becomes

{
  "main" : "value_to_replace",
  "main" : "replaced"
}

which is infact still valid json.


Solution

  • it works for me.

    import simplejson as json
    
    str = """{
      "main" : "value_to_replace"
    }"""
    data = json.loads(str)
    print data
    data["main"] = "test"
    print data
    

    Output:

    (test)alexandr@alexandr:~/Desktop$ python test.py
    {'main': 'value_to_replace'}
    {'main': 'test'}