2

here i m having a json in below format

Present Format

 {
  "info": {
    "laptop": {
    },
    "config": {
      "properties": {
        "ram": {
        },
        "processor": {
        },
        "hdd": {

        }
      }
    },
    "link": {

    },
    "name": {

    },
    "company": {
      "properties": {
        "model": {

        },
        "maker": {
          "type": "integer"
        },
        "country": {
          "type": "text"
        },
        "enterprise": {

        }

      }
    }
  }
}

i am using some plugin like ngx-tree, primeng plugins for which data needs to be in different format like below

Required Format

  [
        {
          name: 'laptop',

        },
        {
          name: 'config',
          children: [
            { name: 'ram', children: [] },
            { name: 'processor' },
            {name:'hdd'}
          ]
        },
         {
          name: 'link',
          children: []
        },
        {
          name: 'name',
          children: []
        },

        {
          name: 'company',
          children: [
            { name: 'model' },
            { name: 'maker' },
            { name: 'country' },
            { name: 'enterprise' }

          ]
        }
      ];

now my issue is how can i change my data from the present format to required format ? is there any suggestion so that i can make that change

2
  • can you update the required format clear? Commented Dec 11, 2018 at 4:22
  • @Harish now it is updated please check Commented Dec 11, 2018 at 4:35

3 Answers 3

1

You can recursively walk over the input object to map it to the required format with something like this:

  function recursiveConvert(input) {
     let converted = []

     for(i in input){
       converted.push({
         name: i,
         children: []
       })

       if(input[i].properties) {
          converted[converted.length - 1].children.push(recursiveConvert(input[i].properties)) 
       }
     }

     return converted
  }

If you want no empty children arrays simply change it to:

function recursiveConvert(input) {
     let converted = []

     for(i in input){
       converted.push({
         name: i
       })

       if(input[i].properties) {
          let lastInsert = converted[converted.length - 1]

          lastInsert.children = [recursiveConvert(input[i].properties)]
       }
     }

     return converted
  }

be sure to call it with recursiveConvert(myInput.info) since it doesn't expect that wrapper object.

Sign up to request clarification or add additional context in comments.

2 Comments

based on our input i created this stack link please check but im not able to get any log stackblitz.com/edit/angular-yljwzd
Like I said call it with this.recursiveConvert(this.data.info), it's not expecting the wrapping info object
1

You can do:

const json = {"info": {"laptop": {},"config": {"properties": {"ram": {},"processor": {},"hdd": {}}},"link": {},"name": {},"company": {"properties": {"model": {},"maker": {"type": "integer"},"country": {"type": "text"},"enterprise": {}}}}};
const result = Object.keys(json.info).map(k => ({
  name: k,
  children: json.info[k].properties
    ? Object.keys(json.info[k].properties).map(kk => ({name: kk}))
    : []
}));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

3 Comments

can it work on n number child ? i mean suppose if a key has nested children and in that also we have nested children
You can do it recursively by keeping track of the current object and check if has children..
i mean like this { name: 'root2', children: [ { name: 'child1' }, { name: 'child2', children: [ { name: 'grandchild1' }, { name: 'grandchild2' } ] } ] } ]; ?
1

Here's a one line, purely functional solution:

const input = {
  "info": {
    "laptop": {
    },
    "config": {
      "properties": {
        "ram": {
        },
        "processor": {
        },
        "hdd": {

        }
      }
    },
    "link": {

    },
    "name": {

    },
    "company": {
      "properties": {
        "model": {
          "properties": {
            "apple": {},
            "banana": {},
            "pear": {}
          }
        },
        "maker": {
          "type": "integer"
        },
        "country": {
          "type": "text"
        },
        "enterprise": {

        }

      }
    }
  }
}

const convert = input => Object
  .entries(input)
  .map(([name, value]) => ({
    name,
    children: convert(value.properties || {})
  }))

console.log(convert(input.info));

Here's an es2015 version:

function convert(input) {
  return Object.keys(input).map(function(name) {
    return {
      name: name,
      children: convert(input[name].properties || {})
    };
  });
}

2 Comments

can it work on n number child ? i mean suppose if a key has nested children and in that also we have nested children
it uses es 2017 right is it possible with es 6 or 5 bcoz in my application it is giving errors

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.