8

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

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?

Eliran Malka
  • 14,498
  • 5
  • 72
  • 96
NEOJPK
  • 319
  • 1
  • 4
  • 12
  • 6
    not clear what is inside-out – Tomer W Jan 31 '17 at 19:06
  • *turn the last element to the first and so on* - this can by applied to arrays but not objects – RomanPerekhrest Jan 31 '17 at 19:07
  • 3
    Can you include the expected output? – Joseph Marikle Jan 31 '17 at 19:07
  • 3
    [There's no such thing as a "JSON Object"](http://benalman.com/news/2010/03/theres-no-such-thing-as-a-json/) – Andreas Jan 31 '17 at 19:09
  • There is only one object within the array. What are you trying to achieve? – guest271314 Jan 31 '17 at 19:09
  • https://gist.github.com/pilsoftTeam/be41bfc0aa2db9c5ae96df8f07cf55ce – NEOJPK Jan 31 '17 at 19:18
  • I made a gist with my expected output.! Feel so dumb now ! i ve read that it cant be done so i asked here ! But seems like it cannot be done if it isnt an array – NEOJPK Jan 31 '17 at 19:20
  • 2
    Even if you order your object by properties i.e. traer_preguntas will be first and id will be the last one and so forth there is no guarantee that the order will stay the same. You need iterate the object and then use it the way you want. [object order](http://stackoverflow.com/questions/5525795/does-javascript-guarantee-object-property-order) – Marina Dunst Jan 31 '17 at 19:20
  • 1
    @NEOJPK - i edited your question a little, for clarity, and nominated this question for repoen – Eliran Malka Feb 01 '17 at 13:53
  • 2
    @NEOJPK voted to reopen post-edits. Be patient and let the process do its thing. – Jared Smith Feb 01 '17 at 13:55
  • 1
    Your desired output is not possible/valid JavaScript. The innermost object needs to have a property name associated with it. – Heretic Monkey Feb 01 '17 at 15:49

1 Answers1

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

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"
                    }
                }
            }
        }
    }
]
Maher Fattouh
  • 1,672
  • 14
  • 22