How to convert json to javascript object?

All we need is an easy explanation of the problem, so here it is.

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

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

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

// the elements one by one
for (let key in objFromJson) {
  objFromJson[key].forEach(item => {

Method 2

Use JSON parser.

var obj = JSON.parse('{ "name":"John", "age":30, "city":"New York"}');

Helpful link

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply