如何反转JSON对象的层次结构?

时间:2022-05-23 19:54:34

How could I "Turn inside out" a JSON object received from a server API using javascript?

我怎么能使用javascript“从里面翻出”从服务器API收到的JSON对象?

example input

[
    {
        "id": 7,
        "idAsignacion": 9,
        "idPregunta": 4,
        "cumplimiento": 1,
        "observacionNumeral": 20,
        "observacionEscrita": "HOLAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
        "rutaObservacionDocumental": "1/1/1/1/4",
        "created_at": "2017-01-31 18:26:44",
        "updated_at": "2017-01-31 18:26:44",
        "traer_preguntas": {
            "id": 4,
            "idRequisito": 1,
            "ordenPreguntas": 1,
            "pregunta": "jojgpofdkñkñdkgñk",
            "tecnicaAuditoria": "Observación",
            "escrita": 1,
            "numeral": 1,
            "documental": 1,
            "estado": 0,
            "created_at": "2017-01-31 15:42:41",
            "updated_at": "2017-01-31 15:42:41",
            "obtener_requisitos": {
                "id": 1,
                "ordenRequisito": 1,
                "idDimension": 1,
                "nombreRequisito": "Requisito uno",
                "estado": 0,
                "created_at": "2017-01-30 15:19:02",
                "updated_at": "2017-01-30 15:19:02",
                "obtener_dimensiones": {
                    "id": 1,
                    "ordenDimension": 1,
                    "dimension": "Dimension UNO",
                    "estado": 0,
                    "created_at": "2017-01-30 15:18:48",
                    "updated_at": "2017-01-30 15:18:48"
                }
            }
        }
    }
]

desired output

"obtener_dimensiones": {
    "id": 1,
    "ordenDimension": 1,
    "dimension": "Dimension UNO",
    "estado": 0,
    "created_at": "2017-01-30 15:18:48",
    "updated_at": "2017-01-30 15:18:48""obtener_requisitos": {
        "id": 1,
        "ordenRequisito": 1,
        "idDimension": 1,
        "nombreRequisito": "Requisito uno",
        "estado": 0,
        "created_at": "2017-01-30 15:19:02",
        "updated_at": "2017-01-30 15:19:02",
        "traer_preguntas": {
            "id": 4,
            "idRequisito": 1,
            "ordenPreguntas": 1,
            "pregunta": "jojgpofdkñkñdkgñk",
            "tecnicaAuditoria": "Observación",
            "escrita": 1,
            "numeral": 1,
            "documental": 1,
            "estado": 0,
            "created_at": "2017-01-31 15:42:41",
            "updated_at": "2017-01-31 15:42:41",
            {
                "id": 7,
                "idAsignacion": 9,
                "idPregunta": 4,
                "cumplimiento": 1,
                "observacionNumeral": 20,
                "observacionEscrita": "HOLAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
                "rutaObservacionDocumental": "1/1/1/1/4",
                "created_at": "2017-01-31 18:26:44",
                "updated_at": "2017-01-31 18:26:44",

            }
        }
    }
}

How to turn the last element to be the first and so on until every "child" property becomes the parent of it's original parent?

如何将最后一个元素变成第一个元素,依此类推,直到每个“child”属性成为它的原始父元素的父元素?

1 个解决方案

#1


3  

Input:

obj = [{
    "id": 7,
    "idAsignacion": 9,
    "idPregunta": 4,
    "cumplimiento": 1,
    "observacionNumeral": 20,
    "observacionEscrita": "HOLAAAAAAAAAAAAAAAAAAAA",
    "rutaObservacionDocumental": "1/1/1/1/4",
    "created_at": "2017-01-31 18:26:44",
    "updated_at": "2017-01-31 18:26:44",
    "traer_preguntas": {
        "id": 4,
        "idRequisito": 1,
        "ordenPreguntas": 1,
        "pregunta": "jojgpofdkñkñdkgñk",
        "tecnicaAuditoria": "Observación",
        "escrita": 1,
        "numeral": 1,
        "documental": 1,
        "estado": 0,
        "created_at": "2017-01-31 15:42:41",
        "updated_at": "2017-01-31 15:42:41",
        "obtener_requisitos": {
            "id": 1,
            "ordenRequisito": 1,
            "idDimension": 1,
            "nombreRequisito": "Requisito uno",
            "estado": 0,
            "created_at": "2017-01-30 15:19:02",
            "updated_at": "2017-01-30 15:19:02",
            "obtener_dimensiones": {
                "id": 1,
                "ordenDimension": 1,
                "dimension": "Dimension UNO",
                "estado": 0,
                "created_at": "2017-01-30 15:18:48",
                "updated_at": "2017-01-30 15:18:48"
            }
        }
    }
}];

The Code:

rd(obj[0])
newObj = {}
kn1 = Object.keys(th[2])
newObj[kn1] = th[2][kn1]
kn2 = Object.keys(th[1])
newObj[kn1][kn2] = th[1][kn2]
kn3 = Object.keys(th[0])
newObj[kn1][kn2][kn3] = th[0][kn3]
newObj[kn1][kn2][kn3].root = thr
console.log(JSON.stringify([newObj], null, 4));

function rd(mo, od) {
    if (typeof(od) === "undefined") {th = {}; od = 0}
    else {od++}
    Object.keys(mo).forEach(function(k) {
        if (typeof(mo[k]) === "object") {
            console.group("depth=" + od + " : " + k + "(" + Object.keys(mo[k]).length + ")");
            rd(mo[k], od)
            console.groupEnd();
            if (typeof(th[od]) === "undefined") th[od] = {}
            th[od][k] = mo[k]
            Object.keys(th[od][k]).forEach(function(ik) {
              if (typeof(th[od][k][ik]) === "object") {
                delete th[od][k][ik]
              }
            })
        } else {
            console.log("depth=" + od + " : " + k)
            if (od === 0) {
                if (typeof(thr) === "undefined") thr = {}
                thr[k] = mo[k]
            }
        }
    });
}

the 2nd part of the code ca be much improved, I can work on it if needed. I added the Key Name root to make the JSON valid

代码的第二部分可以大大改进,如果需要我可以继续工作。我添加了Key Name root以使JSON有效

Output:

[
    {
        "obtener_dimensiones": {
            "id": 1,
            "ordenDimension": 1,
            "dimension": "Dimension UNO",
            "estado": 0,
            "created_at": "2017-01-30 15:18:48",
            "updated_at": "2017-01-30 15:18:48",
            "obtener_requisitos": {
                "id": 1,
                "ordenRequisito": 1,
                "idDimension": 1,
                "nombreRequisito": "Requisito uno",
                "estado": 0,
                "created_at": "2017-01-30 15:19:02",
                "updated_at": "2017-01-30 15:19:02",
                "traer_preguntas": {
                    "id": 4,
                    "idRequisito": 1,
                    "ordenPreguntas": 1,
                    "pregunta": "jojgpofdkñkñdkgñk",
                    "tecnicaAuditoria": "Observación",
                    "escrita": 1,
                    "numeral": 1,
                    "documental": 1,
                    "estado": 0,
                    "created_at": "2017-01-31 15:42:41",
                    "updated_at": "2017-01-31 15:42:41",
                    "root": {
                        "id": 7,
                        "idAsignacion": 9,
                        "idPregunta": 4,
                        "cumplimiento": 1,
                        "observacionNumeral": 20,
                        "observacionEscrita": "HOLAAAAAAAAAAAAAAAAAAAA",
                        "rutaObservacionDocumental": "1/1/1/1/4",
                        "created_at": "2017-01-31 18:26:44",
                        "updated_at": "2017-01-31 18:26:44"
                    }
                }
            }
        }
    }
]

#1


3  

Input:

obj = [{
    "id": 7,
    "idAsignacion": 9,
    "idPregunta": 4,
    "cumplimiento": 1,
    "observacionNumeral": 20,
    "observacionEscrita": "HOLAAAAAAAAAAAAAAAAAAAA",
    "rutaObservacionDocumental": "1/1/1/1/4",
    "created_at": "2017-01-31 18:26:44",
    "updated_at": "2017-01-31 18:26:44",
    "traer_preguntas": {
        "id": 4,
        "idRequisito": 1,
        "ordenPreguntas": 1,
        "pregunta": "jojgpofdkñkñdkgñk",
        "tecnicaAuditoria": "Observación",
        "escrita": 1,
        "numeral": 1,
        "documental": 1,
        "estado": 0,
        "created_at": "2017-01-31 15:42:41",
        "updated_at": "2017-01-31 15:42:41",
        "obtener_requisitos": {
            "id": 1,
            "ordenRequisito": 1,
            "idDimension": 1,
            "nombreRequisito": "Requisito uno",
            "estado": 0,
            "created_at": "2017-01-30 15:19:02",
            "updated_at": "2017-01-30 15:19:02",
            "obtener_dimensiones": {
                "id": 1,
                "ordenDimension": 1,
                "dimension": "Dimension UNO",
                "estado": 0,
                "created_at": "2017-01-30 15:18:48",
                "updated_at": "2017-01-30 15:18:48"
            }
        }
    }
}];

The Code:

rd(obj[0])
newObj = {}
kn1 = Object.keys(th[2])
newObj[kn1] = th[2][kn1]
kn2 = Object.keys(th[1])
newObj[kn1][kn2] = th[1][kn2]
kn3 = Object.keys(th[0])
newObj[kn1][kn2][kn3] = th[0][kn3]
newObj[kn1][kn2][kn3].root = thr
console.log(JSON.stringify([newObj], null, 4));

function rd(mo, od) {
    if (typeof(od) === "undefined") {th = {}; od = 0}
    else {od++}
    Object.keys(mo).forEach(function(k) {
        if (typeof(mo[k]) === "object") {
            console.group("depth=" + od + " : " + k + "(" + Object.keys(mo[k]).length + ")");
            rd(mo[k], od)
            console.groupEnd();
            if (typeof(th[od]) === "undefined") th[od] = {}
            th[od][k] = mo[k]
            Object.keys(th[od][k]).forEach(function(ik) {
              if (typeof(th[od][k][ik]) === "object") {
                delete th[od][k][ik]
              }
            })
        } else {
            console.log("depth=" + od + " : " + k)
            if (od === 0) {
                if (typeof(thr) === "undefined") thr = {}
                thr[k] = mo[k]
            }
        }
    });
}

the 2nd part of the code ca be much improved, I can work on it if needed. I added the Key Name root to make the JSON valid

代码的第二部分可以大大改进,如果需要我可以继续工作。我添加了Key Name root以使JSON有效

Output:

[
    {
        "obtener_dimensiones": {
            "id": 1,
            "ordenDimension": 1,
            "dimension": "Dimension UNO",
            "estado": 0,
            "created_at": "2017-01-30 15:18:48",
            "updated_at": "2017-01-30 15:18:48",
            "obtener_requisitos": {
                "id": 1,
                "ordenRequisito": 1,
                "idDimension": 1,
                "nombreRequisito": "Requisito uno",
                "estado": 0,
                "created_at": "2017-01-30 15:19:02",
                "updated_at": "2017-01-30 15:19:02",
                "traer_preguntas": {
                    "id": 4,
                    "idRequisito": 1,
                    "ordenPreguntas": 1,
                    "pregunta": "jojgpofdkñkñdkgñk",
                    "tecnicaAuditoria": "Observación",
                    "escrita": 1,
                    "numeral": 1,
                    "documental": 1,
                    "estado": 0,
                    "created_at": "2017-01-31 15:42:41",
                    "updated_at": "2017-01-31 15:42:41",
                    "root": {
                        "id": 7,
                        "idAsignacion": 9,
                        "idPregunta": 4,
                        "cumplimiento": 1,
                        "observacionNumeral": 20,
                        "observacionEscrita": "HOLAAAAAAAAAAAAAAAAAAAA",
                        "rutaObservacionDocumental": "1/1/1/1/4",
                        "created_at": "2017-01-31 18:26:44",
                        "updated_at": "2017-01-31 18:26:44"
                    }
                }
            }
        }
    }
]