The source code for the json is from a json file that contains output as shown below
[{
"text": "text to be shown",
"label": [
{
"text": "text to be shown",
"purpose": "scoping",
"starting": 13,
"ending": 20
}
]
}]
I want to remove or ignore the name of the keys within the label attribute, but retain the value within it. So that the final output will read as below
[{
"text": "text to be shown",
"label": [["text to be shown","scoping",13,20]]
}]
CodePudding user response:
Assuming that the json file is saved as 'data.json' and is within the same folder as your script, do:
#run this in python3
import json
with open('data.json', 'r') as f:
data = json.loads(f.read())
data[0]['label'][0] = list(data[0]['label'][0].values())
print(data)
You should see:
[{'text': 'text to be shown', 'label': [['text to be shown', 'scoping', 13, 20]]}]
Why this works: Remember that lists are mutable. Thus you can reassign the list contents in its place and the modifications will be reflected within the same object.
Cheers!