[Solved] Create a JSON reading recursively from another unknown JSON with Javascript


Try it at https://jsfiddle.net/zqkdq5mf/1/

function AppendObject(obj) {
    //console.log(JSON.stringify(obj) + ":" + JSON.stringify(obj).length);

    var Summary = null;
    for (var propertyName in obj) {
    if (typeof obj[propertyName] === "object" && !Summary) {
        Summary = { name: propertyName.toString(), size: JSON.stringify(obj).length, children: [] };

      for (var childPropertyName in obj[propertyName]) {
        var Child = {};
        Child[childPropertyName] = obj[propertyName][childPropertyName];
        Summary.children[Summary.children.length] = AppendObject(Child);
      }
      break;
    } else if (!Summary) Summary = { key: propertyName, value: obj[propertyName], size: JSON.stringify(obj).length };
  }

  return Summary;
}

function Convert() {
    $(".data").empty();
    var ParsedJSON = $.parseJSON($("textarea").val()),
        ObjectArray = [],
        Summaries = [];

    if (!(ParsedJSON instanceof Array)) ObjectArray[0] = ParsedJSON;
    else ObjectArray = ParsedJSON;

    for (var i = 0; i < ObjectArray.length; i++) Summaries[Summaries.length] = AppendObject(ObjectArray[i]);

    $(".data").text(JSON.stringify(Summaries));
}

1

solved Create a JSON reading recursively from another unknown JSON with Javascript