Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

205
Vistas
Change dynamic json structure to key,value and children structure

I have a dynamic JSON structure ex:

{
  "video": {
    "width": 1920,
    "height": 1080,
    "video_codec": "H264",
    "CBR": "4337025",
    "frame_rate": {
      "numerator": 25,
      "denominator": 1
    },
    "specified": {
      "numerator": 1,
      "denominator": 1
    },
    "gop": {
      "length": 50,
      "reference_frames": 3,
      "sub_gop": "StaticType"
    },
    "codec_details": {
      "profile": "Main",
      "level": "Level4",
      "entropy_encoding": "CABAC",
      "video_output": "AVC1"
    }
  }
}

I want to transform it to tree node

export class TrackDetailsNode {
  key: string;
  value: string;
  children?: TrackDetailsNode[];
}

Output Example:

{
  "key": "video",
  "children": [
    {
      "key": "width",
      "value": "1920"
    },
    {
      "key": "frameRate",
      "children": [
        {
          "key": "numerator",
          "value": "60"
        },
        {
          "key": "denominator",
          "value": "1"
        }
      ]
    }
  ]
}

I need it in a recursive way. I want to build a tree from the provided JSON. Tried multiple solution but is taking a long time to parse.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

var obj = {
  "video": {
    "width": 1920,
    "height": 1080,
    "video_codec": "H264",
    "CBR": "4337025",
    "frame_rate": {
      "numerator": 25,
      "denominator": 1
    },
    "specified": {
      "numerator": 1,
      "denominator": 1
    },
    "gop": {
      "length": 50,
      "reference_frames": 3,
      "sub_gop": "StaticType"
    },
    "codec_details": {
      "profile": "Main",
      "level": "Level4",
      "entropy_encoding": "CABAC",
      "video_output": "AVC1"
    }
  }
};

function transform(data, output, children) {
  Object.keys(data).forEach((key) => {
     let value = data[key];
     if (children) {
       if (typeof value === 'object') {
          const child = [];
          children.push({
             'key': key,
             children: child
          });
          transform(value, undefined, child)
       } else {
          children.push({
             'key': key,
             value
          })
       }
     } else {
        if (typeof value === 'object') {
          output.key = key;
          output.children = [];
          transform(value, undefined, output.children);
       } else {
          output.key = key;
          output.value = value;
       }
     }
  });
  return output;
}

console.log(transform(obj, {}));
about 4 years ago · Juan Pablo Isaza Denunciar

0

const INPUT = {
    "video": {
        "width": 1920,
        "height": 1080,
        "video_codec": "H264",
        "CBR": "4337025",
        "frame_rate": {
            "numerator": 25,
            "denominator": 1
        },
        "specified": {
            "numerator": 1,
            "denominator": 1
        },
        "gop": {
            "length": 50,
            "reference_frames": 3,
            "sub_gop": "StaticType"
        },
        "codec_details": {
            "profile": "Main",
            "level": "Level4",
            "entropy_encoding": "CABAC",
            "video_output": "AVC1"
        }
    }
};

const isObject = (obj) => obj !== null && typeof obj === 'object';

const Transform = (input, isChildren) => {
    const output = isChildren ? [] : {};

    if (input && isObject(input)) {
        Object.keys(input).forEach(key => {
            const value = input[key];
            const val = isObject(value) ? Transform(value, true) : value;
            const valueKey = isObject(value) ? "children" : "value";

            if (isChildren) {
                output.push({
                    "key": key,
                    [valueKey]: val
                });
            } else {
                output.key = key;
                output[valueKey] = val;
            }
        })
    }

    return output;
}

const result = Transform(INPUT);
console.log(JSON.stringify(result, null, 2));

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda