I am trying to specify a name for my google spreadsheet api. This is done in the 'title' key value. I have tried with the below but it adds a new key to the existing json. Is there a way to get to the "title": ""
and update that value with the new_date
item?
prev_date = datetime.date.today[]-datetime.timedelta[1]
new_date = str[prev_date.isoformat[]]
res = {
"requests": [
{
"addSheet": {
"properties": {
"title": ""
}
}
}
]
}
res['title'] = new_date
print [res]
This is the output:
{'requests': [{'addSheet': {'properties': {'title': ''}}}], 'title': '2016-12-29'}
This is what I would like it to be:
{'requests': [{'addSheet': {'properties': {'title': '2016-12-29'}}}]}
asked Dec 30, 2016 at 16:26
3
From
the structure you mentioned, the title
key that you need to modify is more nested than what you are providing with.
You need to make the following change:
prev_date = datetime.date.today[]-datetime.timedelta[1]
new_date = str[prev_date.isoformat[]]
res = {
"requests": [
{
"addSheet": {
"properties": {
"title": ""
}
}
}
]
}
res['requests'][0]['addSheet']['properties']['title'] = new_date
print [res]
Where:
'requests'
value is alist
0
is the first item in the list [and the only item]'addSheet'
is the key in the dictionary that is the value of the item in the list in the0
index'properties'
is the key in the above dictionary'title'
is the key in the above dictonary, and the one you need upon your request
answered Dec 30, 2016 at 16:29
Avihoo MamkaAvihoo Mamka
4,5163 gold badges30 silver badges42 bronze badges
You are incorrectly indexing your JSON object and adding a new key named 'title' in the root of the object, while you are trying to update the value inside the array. In your case, you should be accessing res['requests'][0]['addSheet']['properties']['title'] = new_date
answered Dec 30, 2016 at 16:29
I now realize I can pass my variables directly in the json.
prev_date = datetime.date.today[]-datetime.timedelta[1]
new_date = str[prev_date.isoformat[]]
req = {
"requests": [
{
"addSheet": {
"properties": {
"title": new_date
}
}
answered Dec 30, 2016 at 18:54
FrankyFranky
431 gold badge1 silver badge5 bronze badges
how to update json key name while keeping the values in mysql, postgresql update json field key value , parsing through json without using key value python , mysql get json value by key UPDATE `my_table`
SET `my_col` = JSON_INSERT[
JSON_REMOVE[my_col, '$.oldKeyValue'],
'$.newKeyValue',
JSON_EXTRACT[my_col, '$.oldKeyValue']
];
Suggestion : 2
data = { oldName: 1, other: 2 } data.newName = data.oldName delete data.oldName
obj = { name: 'Bobo' } obj.newName = obj.name delete obj.name
obj.newProperty = obj.property delete obj.property
Suggestion : 3
When to use yield instead of return in Python?,Read JSON file using Python,Read, Write and Parse JSON using Python,Working With JSON Data in Python
3._
{ "age": 31, "Salary": 25000, "name": "John" }
Output:
["Welcome", "to", "GeeksforGeeks"] ["Welcome", "to", "GeeksforGeeks"] "Hi" 123 23.572 true false null
Suggestion : 4
Updating a JSON object in Python is as simple as using the built-in update[] function from the json package we have imported.,Converting a byte string or dictionary into JSON is very simple. We just have to use the built-in json.dumps[] function.,In Python, we can use JSON by importing the built-in Python module called json. The json module encodes and decodes JSON data.,While the loads[] method we pass a JSON string that is defined as a Python variable and serializes that string into a JSON object. The following code samples display this functionality.
Code:
{ "date": "2021-07-17", "firstname": "Hamza", "lastname": "Sher", "city": "Kyoto", "array": [ "Carmela", "Ashlee", "Alisha" ], "array of objects": [{ "index": 0, "index start at 5": 5 }, { "index": 1, "index start at 5": 6 }, { "index": 2, "index start at 5": 7 } ] }
Suggestion : 5
update[] method updates the dictionary with elements from a dictionary object or an iterable object of key/value pairs.,It doesn't return any value [returns None].,The update[] method updates the dictionary with the elements from another dictionary object or from an iterable of key/value pairs.,The update[] method takes either a dictionary or an iterable object of key/value pairs [generally tuples].
Example
marks = { 'Physics': 67, 'Maths': 87 } internal_marks = { 'Practical': 48 } marks.update[internal_marks] print[marks] # Output: { 'Physics': 67, 'Maths': 87, 'Practical': 48 }
Example
marks = { 'Physics': 67, 'Maths': 87 } internal_marks = { 'Practical': 48 } marks.update[internal_marks] print[marks] # Output: { 'Physics': 67, 'Maths': 87, 'Practical': 48 }
The syntax of update[]
is:
Example 1: Working of update[]
d = { 1: "one", 2: "three" } d1 = { 2: "two" } # updates the value of key 2 d.update[d1] print[d] d1 = { 3: "three" } # adds element with key 3 d.update[d1] print[d]