How could I "Turn inside out" a JSON object received from a server API using javascript?
[
{
"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"
}
}
}
}
]
"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?
The JSON RFC (RFC 4627) says that order of object members does not matter.
JavaScript Object Notation (JSON) is a standard text-based format for representing structured data based on JavaScript object syntax. Extension is . json , and MIME type is application/json . It's broadly used in data transaction between application and servers. JS only has one data type which is Object.
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"
}
}
}
}];
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
[
{
"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"
}
}
}
}
}
]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With