I want to know how to modify a .json, add new elements and save it in the same.
I have this data.json:
{
"utts": {
"FADG0_SI1279": {
"input": [
{
"feat": "/content/espnet/egs/timit/asr1/dump/train_dev/deltafalse/feats.1.ark:13",
"name": "input1",
"shape": [
180,
26
]
}
],
"output": [
{
"name": "target1",
"shape": [
23,
42
],
"text": "sil b r ih sil k s aa r er n aa l sil t er n ih sil t ih v sil",
"token": "<space> b r ih <space> k s aa r er n aa l <space> t er n ih <space> t ih v <space>",
"tokenid": "2 8 30 19 2 22 31 3 30 14 25 3 23 2 33 14 25 19 2 33 19 37 2"
....
I want to modify it and put the character "|" between each element of the "text", like this:
],
"text": "sil | b | r | ih | sil | k | s | aa | r | er | n | aa | l | ..."
...´´´
CodePudding user response:
As the first thing, you should convert your .json
file to a Python dict
:
import json
with open("data.json", 'w ') as file:
myData = json.load(file)
and then you have to use it as a Python dict
, and get the str
ing you need:
myData["utts"]["output"][0]["text"] # Your string
and then you have to operate over the string:
myData["utts"]["output"][0]["text"] = ' | '.join(myData["utts"]["output"][0]["text"].split())
and then to code your dict
back to a JSON
object:
json.dump(myData, file)
CodePudding user response:
If your text
key is ALWAYS separated by one whitespace you could:
import json
with open('data.json') as f:
data = json.load(f)
data['utts']['output']['text'] = data['utts']['output']['text'].replace(" ", " | ")
with open('data.json', 'w') as f:
json.dump(data)
But that only works if you have one whitespace only as separator.
From there you can think about another edge cases.