I am using github api and got the content of manifest.json file.
I am trying to add some new json lines into that existing json folder.
This the old content which is existing
{
"short_name": "React App",
"name": "Create React App Sample",
"icons": [
{
"src": "favicon.ico",
"sizes": "64x64 32x32 24x24 16x16",
"type": "image/x-icon"
},
{
"src": "logo192.png",
"type": "image/png",
"sizes": "192x192"
},
{
"src": "logo512.png",
"type": "image/png",
"sizes": "512x512"
}
],
"start_url": ".",
"display": "standalone",
"theme_color": "#000000",
"background_color": "#ffffff"
}
this is the expected content output , after adding those new json contents it should look like this
{
"short_name": "React App",
"name": "Create React App Sample",
"icons": [
{
"src": "maskable.png",
"sizes": "280x280",
"type":"image/png",
"purpose": "any maskable"
},
{
"src": "logo192.png",
"sizes": "192x192",
"type": "image/png"
},
{
"src": "logo256.png",
"sizes": "256x256",
"type": "image/png"
},
{
"src": "logo348.png",
"sizes": "348x348",
"type": "image/png"
},
{
"src": "logo512.png",
"sizes": "512x512",
"type": "image/png"
}
],
"start_url": ".",
"display": "standalone",
"theme_color": "#000000",
"background_color": "#ffffff"
}
I tried string.replace but it is not working
let newContent = oldContent.replace(`{"src": "favicon.ico","sizes": "64x64 32x32 24x24 16x16","type": "image/x-icon"},`,
`{"src": "logo256.png","sizes": "256x256","type": "image/png"},{"src": "logo348.png","sizes": "348x348", "type": "image/png"},`);
CodePudding user response:
You have to read data from json file first.After reading data you can use JSON.parse(filedata)
method to convert your data to java script object.
After converting you can replace data by manipulating JavaScript object.After Replacing the data you have to stringfy the java script objects using JSON.stringfy(data)
.And last step to write it again in file.
CodePudding user response:
Instead of manipulating strings, you can convert it to an object and add values:
var json_object = JSON.parse(`{"src": "favicon.ico"...`);
json_object.icons = "{
"src": "maskable.png",
"sizes": "280x280",
"type":"image/png",
"purpose": "any maskable"
}"