I have a JSON which contains directory structure and needs to convert to javascript object so that a map for key-value pair can be formed from it.
{
"/Inbound/tmp/ARTIFACTDOWNLOAD_DIR": [
{
"filePath": "/Inbound/tmp/ARTIFACTDOWNLOAD_DIR",
"fileName": "package_installation_logs.txt",
"lastModifiedDate": "06-14-2019 09:34:43",
"sizeofFile": "0 KB",
"type": "File"
}
],
"/Inbound": [
{
"filePath": "/Inbound",
"fileName": "osqueryd.INFO",
"lastModifiedDate": "06-14-2019 09:23:26",
"sizeofFile": "1 KB",
"type": "File"
},
{
"filePath": "/Inbound",
"fileName": "tmp",
"lastModifiedDate": "06-14-2019 10:51:55",
"sizeofFile": "",
"type": "Folder"
}
]
}
Here key-value pairs in JSON contain different keys. So, how I can map key-value to javascript object using typescript
Simply use it like this
// your JSON - compressed const json = '{"/Inbound/tmp/ARTIFACTDOWNLOAD_DIR":[{"filePath":"/Inbound/tmp/ARTIFACTDOWNLOAD_DIR","fileName":"package_installation_logs.txt","lastModifiedDate":"06-14-2019 09:34:43","sizeofFile":"0 KB","type":"File"}],"/Inbound":[{"filePath":"/Inbound","fileName":"osqueryd.INFO","lastModifiedDate":"06-14-2019 09:23:26","sizeofFile":"1 KB","type":"File"},{"filePath":"/Inbound","fileName":"tmp","lastModifiedDate":"06-14-2019 10:51:55","sizeofFile":"","type":"Folder"}]}' const objFromJson = JSON.parse(json) // the whole object console.log(objFromJson) console.log(objFromJson['/Inbound/tmp/ARTIFACTDOWNLOAD_DIR']) // the elements one by one for (let key in objFromJson) { objFromJson[key].forEach(item => { console.log(item) }) }
Use JSON parser.
var obj = JSON.parse('{ "name":"John", "age":30, "city":"New York"}');
Helpful link https://www.w3schools.com/js/js_json_parse.asp
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.