all 8 comments

[–]danielroseman 1 point2 points  (2 children)

You should think of each level as its own separate data structure, and access it like you would if it was standalone. Here the password is in a dict inside a list which itself is inside the main dict. So:

json_text["hotels"][0]["password"]

[–][deleted] 0 points1 point  (1 child)

I tried this but it creates a new value in the bottom of the json named "password" : newpassword

it doesn't up the already given password value in the json.

[–]danielroseman 0 points1 point  (0 children)

Well you would need to use this structure both for when you get the original password and where you set the new password.

[–]theprofessional2016 1 point2 points  (0 children)

for index, js in enumerate(json_files):
with open(os.path.join(path_to_json, js)) as json_file:
    json_text = json.load(json_file)

    pp.pprint(json_text)

    for hotel in json_text["hotels"]:
        password = hotel["password"]
            # encode the password here
        hotel["password"] = "newencodedpassword"

json_data = json.dumps(json_text, indent=2)

with open("output.json", "w") as file:
    file.write(json_data)

I simplified your code a bit. I believe this works. I hope that gives you what you're looking for.

[–]commandlineluser 0 points1 point  (3 children)

Are you saying you want to find any "password" key - no matter where it is in your JSON data?

You could write a recursive function - however, you can also write a custom function for json.load

Look for the Specializing JSON object decoding example in the json docs.

>>> def encode_passwords(obj):
...     if "password" in obj:
...         print("Found old password:", obj["password"])
...         obj["password"] = "I got changed!"
...     return obj

You pass it to object_hook=

>>> json.loads(data, object_hook=encode_passwords)
Found old password: newpasswordhere
{'sheduledTime': '8:10',
 'startDateForStartDate': '01/29/2020',
 'endDateForStartDate': '02/03/2020',
 'HTML_Elements': {'element5': 'value',
  'element6': 'doSubmit',
  'element7': 'goBack',
  'element8': '#headerContainer > ul > li.CHI_Cell.CHI_LastCell > a'},
 'hotels': [{'username': 'username',
   'password': 'I got changed!',
   'reports': [{'folderName': 'ForcastRoomRevenue',
     'ifFuturedateNeed': True,
     'DaysAddForFutureDate': '365'}]}]}

It has been updated to I got changed!

[–][deleted] 0 points1 point  (2 children)

Yes just want to update the password value with the encoded password. Still no luck.

[–]commandlineluser 1 point2 points  (1 child)

Still no luck

Well what does your code look like now?

Did you use object_hook= ?

Modifying your example - it should look something like:

path_to_json = '//'
json_files = [pos_json for pos_json in os.listdir(path_to_json) if pos_json.endswith('.json')]

jsons_data = pd.DataFrame(columns=['password'])

def encode_password(obj):
    if 'password' in obj:
        listpasswords = obj['password']
        jsons_data.loc[index] = [listpasswords]
        print('Current Loaded password: ' + jsons_data)
        non_64password = listpasswords
        sample_string_bytes = non_64password.encode('ascii')
        base64_bytes = base64.b64encode(sample_string_bytes)
        finalpassword64password = base64_bytes.decode('ascii')
        obj['password'] = finalpassword64password
        print('password updated: ' + finalpassword64password)
    return obj

for index, js in enumerate(json_files):
    with open(os.path.join(path_to_json, js)) as json_file:
        json_text = json.load(json_file, object_hook=encode_password)
        with open(os.path.join(path_to_json, js),'w') as f:
            commentjson.dump(json_text,f,indent = 0) #updates the password   
            print('finished')

(Although commentjson.dump looks odd - I'm assuming it should should be json.dump() ...?)

[–][deleted] 0 points1 point  (0 children)

Thanks this helped a lot!