Update Key Value In Python In JSON File

2024/7/6 23:02:49

How do I change a value in a json file with python? I want to search and find "class": "DepictionScreenshotsView" and replace it with "class": ""

JSON File:

{"minVersion": "0.1","class": "DepictionTabView","tintColor": "#2cb1be","headerImage": "","tabs": [{"tabname": "Details","class": "DepictionStackView","tintColor": "#2cb1be","views": [{"class": "DepictionSubheaderView","useBoldText": true,"useBottomMargin": false,"title": "Description"},{"class": "DepictionMarkdownView","markdown": "Some dummy text...","useRawFormat": true},{"class": "DepictionSeparatorView"},{"class": "DepictionSubheaderView","useBoldText": true,"useBottomMargin": false,"title": "Screenshots"},{"class": "DepictionScreenshotsView","itemCornerRadius": 6,"itemSize": "{160, 284.44444444444}","screenshots": [{"accessibilityText": "Screenshot","url": "http://example.com/image.png"}]},{"class": "DepictionSeparatorView"},{"class": "DepictionSubheaderView","useBoldText": true,"useBottomMargin": false,"title": "Information"},{"class": "DepictionTableTextView","title": "Author","text": "User"},{"class": "DepictionSpacerView","spacing": 16},{"class": "DepictionStackView","views": [{"class": "DepictionTableButtonView","title": "Contact","action": "http://example.com/","openExternal": true}]},{"class": "DepictionSpacerView","spacing": 16}]},{"tabname": "History","class": "DepictionStackView","views": [{"class": "DepictionSubheaderView","useBoldText": true,"useBottomMargin": false,"title": ""},{"class": "DepictionMarkdownView","markdown": "<ul>\n<li>Initial release.<\/li>\n<\/ul>","useRawFormat": true}]}]
}
Answer

You can read a json file in python as follow:

import json
with open('your_file.json') as f:data = json.load(f)

Then you access and change the value (for your case):

data['tabs'][0]['views'][4]['class'] = ""

After having changed the data, you can save it :

with open('your_file.json', 'w') as outfile:json.dump(data, outfile)
https://en.xdnf.cn/q/119971.html

Related Q&A

Getting UnboundLocalError: local variable age referenced before assignment error

Ive written a simple script with TKinter and SQLAlchemy, where an sqlite .db is created, saving Employee information such as name, age, address, etc, etc.I realized that if a user puts a string in the …

Cannot run python script [duplicate]

This question already has answers here:What does "SyntaxError: Missing parentheses in call to print" mean in Python?(11 answers)Closed 9 years ago.I am trying to run this python script:https…

I want to read multiple audio files with librosa and then save it into an empty list

Here id my code . when I append into the array the array remain empty . Please help me where is the mistake. Or tell me some other way also to do thisA = [] # load more files with librosa pathAudio = …

How to db.execute in postgresql using the LIKE operator with variables within flask [duplicate]

This question already has an answer here:CS50: LIKE operator, variable substitution with % expansion(1 answer)Closed 4 years ago.Im trying to get my db.execute to work but encounter a syntax error when…

C, Perl, and Python similar loops different results

I wrote scripts to calculate pi in python, perl and c. They all use the same algorithm (trapezoidal reimann sum of a circle with n subintervals) and the python and perl programs always get the same re…

How to print a single backslash in python in a string? [duplicate]

This question already has answers here:Why do backslashes appear twice?(2 answers)Closed 1 year ago.In python (3x version)\ # this is error no doubt \\ # this prints two backslashes ,i.e., \\ r\ # thi…

Return nested JSON item that has multiple instances

So i am able to return almost all data, except i am not able to capture something like this:"expand": "schema""issues": [{"expand": "<>","id…

What request should I make to get the followers list from a specific profile page

I am trying to get the followers list from this profile, I tried making a GET request using python requests to the API using this request URL but it didnt seem to work, I got a METHOD_NOT_ALLOWED error…

PlayerDB API Post Requests bring 404

I made a little script to get the UUIDs of people who joined my minecraft server, then run them through the PlayerDB API through a post request to: https://playerdb.co/api/player/minecraft/* where the …

How to make changes using Configparser in .ini file persistent

How to modify the .ini file? My ini file looks like this. And i want the format section ini to be changed like this[Space to be replaced with a tab followed by $] Format="[%TimeStamp%] $(%Thre…