This paper proposes a new over-identifying restriction test called the diagonal J test in the generalized method of moments (GMM) framework. Different from the conventional over-identifying restriction test, where the sample covariance matrix of moment conditions is used in the weighting matrix, the proposed test uses a block diagonal weighting matrix constructed from the efficient optimal weighting matrix. We show that the proposed test statistic asymptotically follows a weighted sum of chi-square distributions with one degree of freedom. Since we need to decompose the moment conditions into groups when implementing the proposed test, we propose two methods to split the moment conditions. The first is to use K-means method that is widely used in the cluster analysis. The second is to utilize the special structure of moment conditions where they are available sequentially. Such a case typically appears in the panel data models. We also conduct a local power analysis of the diagonal J test in the context of dynamic panel data models, and show that the proposed test has almost the same power as the standard J test in some specific cases. Monte Carlo simulation reveals that the proposed test has substantially better size property than the standard test does, while having almost no power loss in comparison to the standard test when it has no size distortions.