Extract deep down data from JSON Object

Viewed 81
{
  "hoodie": {
    "body": {
      "default": {
        "colors": {
          "style": {
            "solid": {
              "colors": {
                "body": "body"
              }
            }
          }
        }
      },
      "additional": {
        "logo": {
          "image": ""
        },
        "chest-pocket": {
          "colors": {
            "chest-pocket": "chest-pocket"
          }
        },
        "tag": {
          "image": ""
        }
      }
    },
    "sleeves": {
      "default": {
        "colors": {
          "style": {
            "solid": {
              "colors": {
                "sleeve": "sleeve"
              }
            }
          }
        }
      },
      "additional": {
        "cuffs": {
          "colors": {
            "cuffs": "cuffs"
          }
        }
      }
    }
  }
}

The format in which I needed is to extract the additional keys. As in this case, hoodie key contains body and sleeves as its key which in the additional contains its different keys corresponding to the additional key. I have given the Object that needs to be parsed and the data obtained after parsing.

{
  "body": ["logo", "chest-pocket", "tag"],
  "sleeves": ["cuffs"]
}

Anyone can please share the link it might help me to play with Objects. Thanks in advance.

2 Answers

You can use array.reduce to get the desired output with the help of Object.keys

Kindly check the below snippet code.

const input = {
  "hoodie": {
    "body": {
      "default": {
        "colors": {
          "style": {
            "solid": {
              "colors": {
                "body": "body"
              }
            }
          }
        }
      },
      "additional": {
        "logo": {
          "image": ""
        },
        "chest-pocket": {
          "colors": {
            "chest-pocket": "chest-pocket"
          }
        },
        "tag": {
          "image": ""
        }
      }
    },
    "sleeves": {
      "default": {
        "colors": {
          "style": {
            "solid": {
              "colors": {
                "sleeve": "sleeve"
              }
            }
          }
        }
      },
      "additional": {
        "cuffs": {
          "colors": {
            "cuffs": "cuffs"
          }
        }
      }
    }
  }
}

let output = Object.keys(input.hoodie).reduce((acc, curr) => {
  acc[curr] = Object.keys(input.hoodie[curr].additional)
return acc 
}, {})

console.log(output)

Regarding objects as trees, you need to iterate over property keys at different levels of the source object. If the paths to the subtrees of interest are fixed, select the respective subtrees and collect keys with the Object.keys method.

Specifically:

Object.keys(objSource).forEach ( (ps_key_toplevel) => {
    objTarget[ps_key_toplevel] = {};
    Object.keys(objSource[ps_key_toplevel]).forEach ( ( ps_key_parts ) => {
        objTarget[ps_key_toplevel][ps_key_parts] = Object.keys(objSource[ps_key_toplevel][ps_key_parts].additional);
    });
});

Example

Full-fledged mapping with the supplied data:

    const objSource = 
      {
        "hoodie": {
          "body": {
            "default": {
              "colors": {
                "style": {
                  "solid": {
                    "colors": {
                      "body": "body"
                    }
                  }
                }
              }
            },
            "additional": {
              "logo": {
                "image": ""
              },
              "chest-pocket": {
                "colors": {
                  "chest-pocket": "chest-pocket"
                }
              },
              "tag": {
                "image": ""
              }
            }
          },
          "sleeves": {
            "default": {
              "colors": {
                "style": {
                  "solid": {
                    "colors": {
                      "sleeve": "sleeve"
                    }
                  }
                }
              }
            },
            "additional": {
              "cuffs": {
                "colors": {
                  "cuffs": "cuffs"
                }
              }
            }
          }
        }
      };

    let objTarget = {}
      ;

    Object.keys(objSource).forEach ( (ps_key_toplevel) => {
        objTarget[ps_key_toplevel] = {};
        Object.keys(objSource[ps_key_toplevel]).forEach ( ( ps_key_parts ) => {
            objTarget[ps_key_toplevel][ps_key_parts] = Object.keys(objSource[ps_key_toplevel][ps_key_parts].additional);
        });
    });
    
    console.log ( objTarget );

Related