0

I have an object received in response from backend, and would like to extract elements of object and attach them to scope to make it available in the View. Following is the structure of the object:

{
    "Name": {
        "S": "Jon Snow"
    },
    "Location": {
        "S": "Winterfell"
    },
    "Details": {
        "M": {
            "Parents": {
                "M": {
                    "mother": {
                        "S": "Lynna Stark"
                    }
                }
            },
            "Dog": {
                "N": "Ghost Snow"
            }
        }
    }
}

Since I have received it from backend, I don't know what kind of object is this, and I want to convert it to a plain JSON object which should be looking something like this:

{
"Name": "Jon Snow",
"Location": "Winterfell",
"Details": {
    "Parents": {
        "mother": "Lynna Stark"
    },
    "Dog": "Ghost Snow"
}

}

Help is appreciated, as I am a beginner in AngularJS and also It would be good if someone elaborates what kind of Object did I receive? Thanks in advance.

Update 1: Thanks for the responses. Now I have got the idea. Now the question is how to I flatten the object by one level? And If I do flatten does it tamper the original response as it is received from the backend, it may be different every time.

The Head Rush
  • 3,157
  • 2
  • 25
  • 45
StandardProc
  • 85
  • 1
  • 8
  • 2
    [There's no such thing as a "JSON Object"](http://benalman.com/news/2010/03/theres-no-such-thing-as-a-json/) – Andreas Oct 22 '20 at 12:43
  • 2
    [Working with objects - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects) – Andreas Oct 22 '20 at 12:44
  • 1
    you always get one letter key when you need to make one depth less ? – Robert Oct 22 '20 at 12:46
  • 1
    "what kind of Object did I receive?" There is no special term for the the kind of object you received. – Heretic Monkey Oct 22 '20 at 12:46
  • 1
    "Convert Object type to JSON object" --- shuold be " Object manipulation flattening object in js" and t this don't have nothing common with Angular – Robert Oct 22 '20 at 12:49
  • Thanks for the responses everyone. Yep flattening should be the question. Now how do I flatten? And reducing the object by one level might tamper with original objects forme? As it is received from backend, it may be different in different cases. – StandardProc Oct 22 '20 at 12:56
  • Your backend response looks like a dynamo db object, and as such it is somewhat predictable in the format. – Adam Specker Oct 22 '20 at 13:01

1 Answers1

0
const data = {
    "Name": {
        "S": "Jon Snow"
    },
    "Location": {
        "S": "Winterfell"
    },
    "Details": {
        "M": {
            "Parents": {
                "M": {
                    "mother": {
                        "S": "Lynna Stark"
                    }
                }
            },
            "Dog": {
                "N": "Ghost Snow"
            }
        }
    }
}

const flatten = (data) =>{
  if(typeof data === "string") return data;
  for(let key in data){
    for(let deep in data[key]){
      if(deep.length === 1){
        const temp = data[key]
        data[key] = flatten(temp[deep])
      }
    }

  }
  return data;
}

console.log( JSON.stringify(flatten(data), null, "\t"))

JS bin

Robert
  • 2,538
  • 1
  • 9
  • 17