1

I want to group Dynamic Object keys of a json. The object values from each keys will be group to their respective key.

I tried using map and reduce to group it but the results are not grouped as I expected.

Here is my JSON Object

var data = [
  {
    "type": 6,
    "data": {
      "error": {
        "cause": {
          "root": {
            "Extracted": {
              "Body": {
                "Error": {
                  "ErrorCode": "143",
                  "ErrorString": "NotFound",
                  "info": {
                    "Error": {
                      "errorDesc": "Data Not Found",
                      "subs": {
                        "attrib": {
                          "subs_name": "123com",
                          "subs_no": 4
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    },
    "status": true
  },
  {
    "type": 6,
    "data": {
      "error": {
        "cause": {
          "root": {
            "Extracted": {
              "Body": {
                "Error": {
                  "ErrorCode": "143",
                  "ErrorString": "NotFound",
                  "info": {
                    "Error": {
                      "errorDesc": "Company Not Found",
                      "subs": {
                        "attrib": {
                          "subs_name": "QRS",
                          "subs_no": 4
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    },
    "status": true
  },
  {
    "type": 6,
    "data": {
      "error": {
        "cause": {
          "root": {
            "Extracted": {
              "Body": {
                "Error": {
                  "ErrorCode": "123",
                  "ErrorString": "SystemFailure",
                  "info": {
                    "Error": {
                      "errorDesc": "Internal server error",
                      "subs": {
                        "attrib": {
                          "subs_name": "ABC",
                          "subs_no": 2
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    },
    "status": true
  },
  {
    "type": 6,
    "data": {
      "error": {
        "cause": {
          "root": {
            "Extracted": {
              "Body": {
                "Error": {
                  "ErrorCode": "123",
                  "ErrorString": "SystemFailure",
                  "info": {
                    "Error": {
                      "errorDesc": "Insufficient Data",
                      "subs": {
                        "attrib": {
                          "subs_name": "DEF",
                          "subs_no": 3
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    },
    "status": true
  },
  {
    "type": 6,
    "data": {
      "error": {
        "cause": {
          "root": {
            "Extracted": {
              "Body": {
                "Error": {
                  "ErrorCode": "999",
                  "ErrorString": "Unknown",
                  "info": {
                    "Unknown": {
                      "desc": "UnknownError",
                      "subs": "GHI"
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    },
    "status": true
  }
]

This is the code that I have tried but didn't get the result I want

var x = data.map((e) => {
  var el = {}
    el[e.data.error.cause.root.Extracted.Body.Error.ErrorString] = 
    [e.data.error.cause.root.Extracted.Body.Error.info];

    return el;
  })

 console.log(x);

The result I got:

 [
   {
     "NotFound": {....}
   },
   {
     "NotFound": {....}
   },
   {
     "SystemFailure": {....}
   },
   {
     "SystemFailure": {....}
   },
   {
     "Unknown": {....}
   },
 ]

The result I expect:

[
 {
   "NotFound": [
     {
       "Error": {
         "errorDesc": "Data Not Found",
         "subs": {
           "attrib": {
              "subs_name": "123com",
              "subs_no": 4
         }
       }
     },
     {
       "Error": {
         "errorDesc": "Company Not Found",
         "subs": {
         "attrib": {
            "subs_name": "QRS",
            "subs_no": 4
         }
       }
     }
   }
  ]
 },
 { 
   "SystemFailure": [
      {
       "Error": {
          "errorDesc": "Internal server error",
          "subs": {
              "attrib": {
              "subs_name": "ABC",
              "subs_no": 2
            }
          }
      },
      {
        "Error": {
            "errorDesc": "Insufficient Data",
            "subs": {
               "attrib": {
                  "subs_name": "DEF",
                  "subs_no": 3
               }
            }
      }
   ]
 },
 {
    "Unknown": [
       {
          "Unknown": {
            "desc": "UnknownError",
            "subs": "GHI"
          }
       }
    ]
 }
]
2

You could reduce the array. Create an accumulator object with unique ErrorString as key. Set the value to be an object with the same ErrorString as key. Then add each info object based on the ErrorString. Use Object.values() get the grouped values as an array

const data=[{type:6,data:{error:{cause:{root:{Extracted:{Body:{Error:{ErrorCode:"143",ErrorString:"NotFound",info:{Error:{errorDesc:"Data Not Found",subs:{attrib:{subs_name:"123com",subs_no:4}}}}}}}}}}},status:true},{type:6,data:{error:{cause:{root:{Extracted:{Body:{Error:{ErrorCode:"143",ErrorString:"NotFound",info:{Error:{errorDesc:"Company Not Found",subs:{attrib:{subs_name:"QRS",subs_no:4}}}}}}}}}}},status:true},{type:6,data:{error:{cause:{root:{Extracted:{Body:{Error:{ErrorCode:"123",ErrorString:"SystemFailure",info:{Error:{errorDesc:"Internal server error",subs:{attrib:{subs_name:"ABC",subs_no:2}}}}}}}}}}},status:true},{type:6,data:{error:{cause:{root:{Extracted:{Body:{Error:{ErrorCode:"123",ErrorString:"SystemFailure",info:{Error:{errorDesc:"Insufficient Data",subs:{attrib:{subs_name:"DEF",subs_no:3}}}}}}}}}}},status:true},{type:6,data:{error:{cause:{root:{Extracted:{Body:{Error:{ErrorCode:"999",ErrorString:"Unknown",info:{Unknown:{desc:"UnknownError",subs:"GHI"}}}}}}}}},status:true}];

const merged = data.reduce((acc, o) => {
  const e = o.data.error.cause.root.Extracted.Body.Error;
  acc[e.ErrorString] = acc[e.ErrorString] || { [e.ErrorString]: [] };
  acc[e.ErrorString][e.ErrorString].push(e.info)
  return acc;
}, {})

const output = Object.values(merged);

console.log(output)

  • 1
    Thank you @adiga ! Can you explain to me how the code works per line? by commenting on the code? Im not really familiar on how to use reduce in json. – vashlor Aug 13 at 14:55

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.