In my application, I am receiving a complex JSON object (like the next one):

    "MainObject": {
        "Object1": [
                "ID": 1234,
                "Name": "Something1",
                "Weight": 4,
                "WeightPercent": 20.44,
                "Height": 21,
                "HeightPercent": 35.32,
                "Object2": [
                        "ID": 4567,
                        "Date": "2018-06-17T22:28:56.782Z",
                        "Object3": [
                                "ID": 6789,
                                "Quantity": 24,
                                "Object4": {
                                    "ID": 1597,
                                    "Name": "Something2",
                                    "Description": "SomeDescription1"
                                "ID": 1578,
                                "Quantity": 10,
                                "Object4": {
                                    "ID": 1111,
                                    "Name": "Something3",
                                    "Description": "SomeDescription2"
                                "ID": 4444,
                                "Quantity": 2,
                                "Object4": {
                                    "ID": 2013,
                                    "Name": "Something4",
                                    "Description": "SomeDescription3"
    "MetaData": {
        "Info1": "Something",
        "Hash": "someHash1234fc55"

And I need to separate it into simple objects in order for my app to accept them and call the method to insert into the database...

What I've tough of is:

  1. copy the nested object into another variable to play with - let's name it data (in order to keep the original data unchanged)
  2. make data = data.MainObject (get rid of the Meta stuff as I don't need it in my app)
  3. create 4 arrays, one for each object:
    let Object1Array:[] = [];
    let Object2Array:[] = [];
    let Object3Array:[] = [];
    let Object4Array:[] = [];
  1. use a for..in in Object1 to iterate through the items in this object
  2. inside 3., use a for..in to iterate through the items in Object2
  3. (same as 4., just for Object3)
  4. insert Object4 into the Object4Array, them delete it from the data object.
  5. after finishing the last for..in (describe at point 6.), use the same principle as in point 7., but for Object3 (insert Object3 into the Object3Array, them delete it from the data object)
  6. same as point 8., just for Object2
  7. same as point 8., but for Object1
  8. the method will return all the arrays, like:
    let returnedData = [Object1Array, Object2Array, Object3Array, Object4Array];

    return returnedData;

As you can see, the above method is quite.. complicated and there are many possibilities for it to fail (and it's also hard to track)

I am looking for a better alternative to do this (maybe with an npm package?)

PS: working with Typescript/Javascript and Node.js

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.