0

Json file example = {"process":[{"id":0,"path":"xyz"}, {"id":1,"path":"abc"}]}

  with open(process_details.json,"r") as jsonfile:
      write_data= json.load(jsonfile)

  for processdetails in write_data['process']:

      Path = function_to_retreive_path(processdetails['id'])

      Write_data[processdetails]['path'] = Path

      with open("process_details.json","w") as outfile:
         json.dump(write_data,outfile)

As I'm trying to update the path for each process I'm able to retrieve the path but I'm unable to update it in process_details.json file. Getting the following error as unhashable type:'dict'

Can anyone advise me on this issue

4
  • The snippet is crawling with typos. Commented Dec 27, 2022 at 13:50
  • with open(process_details.json,"r") as json — overwriting json just before using it Commented Dec 27, 2022 at 13:51
  • Also look at the tool jq. You might not need python at all Commented Dec 27, 2022 at 13:55
  • Adding what write_data is just before dumping to the question can help us provide answers without the need to include missing functions. (print(write_data)) Commented Dec 27, 2022 at 14:03

2 Answers 2

1

Try this:

import json

def function_to_retreive_path (id):
    return str (id) + "_updated"

with open("process_details.json","r") as f:
    write_data = json.load(f)

for processdetails in write_data['process']:

    # debug
    print (processdetails)
    print (processdetails['id'])
    print (processdetails['path'])

    # change value
    path = function_to_retreive_path (processdetails['id'])
    processdetails['path'] = path

    # debug
    print (processdetails['path'])

with open("process_details.json","w") as outfile:
     json.dump(write_data,outfile)

Anyway, your error is caused by a typo. You are using Write_data with a capital W, so Python thinks, I don't know this variable, so it is no dictionary and thus not hashable.

Sign up to request clarification or add additional context in comments.

2 Comments

# change the values write_data['process'][0]['path'] = "foo" write_data['process'][1]['path'] = "bar". Is there a way that we could get the [0] , [1] inside a loop? Since the json is dynamic I can't hardcode the values?
right, I updated the code snippet
0

I can't really answer because there are some missing functions in your code so I can't run it, but the error you are facing generally means that you are using a dictionary as a key in an other dictionary.

2 Comments

Oh that's just a function will retrieve string you can hardcode it for testing
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.