shahzaib
shahzaib

Reputation: 3

How to restructure the given tree json to the output in javascript

I have a JSON file that comes in a particular structure (see Tree), but I need it too be structured like expected output.

Is it possible to reorganise the data in JS? If so, how do you go about this? I need help in restructuring or mapping the tree to the expected output. I hope you guys can help me with this restructuring problem.

const tree = [
      {
        "type": "object", 
        "name": "pets",
        "child": 
        [
          {
            type: "array",
            name: "properties",
            "child": 
            [
              {
                type: "object",
                name: "PK",
              },
              {
                type: "object",
                name: "PO",
              },
              {
                type: "object",
                name: "PS",
              },
              {
                type: "object",
                name: "PA",
                child: [{type: "array", name: "list"}, ]
              },
            ]
          },
          {
            type: "object",
            name: "get",
          }
        ]
      },
    ]
const expectedResult = [
  {
    pets: 
    {
      properties: 
      [
        {
          name: "PK"
        }, 
        {
          name: "PO"
        }, 
        {
          name: "PS"
        }, 
        {
          "PA" : 
          {
            list: []
          }
        }
      ],
      get: {}
    }
  },
]

Upvotes: 0

Views: 60

Answers (3)

Nina Scholz
Nina Scholz

Reputation: 386540

You could take an object for the various types and their functions to build the wanted structure for mapping children.

const
    tree = [{ type: "object", name: "pets", child: [{ type: "array", name: "properties", child: [{ type: "object", name: "PK" }, { type: "object", name: "PO" }, { type: "object", name: "PS" }, { type: "object", name: "PA", child: [{ type: "array", name: "list" }] }] }, { type: "object", name: "get" }] }],
    types = {
        object: (name, child) => child
            ? { [name]: Object.assign({}, ...child.map(fn)) }
            : { name: name },
        array: (name, child = []) => ({ [name]: child.map(fn) })
    },
    fn = ({ type, name, child }) => types[type](name, child),
    result = tree.map(fn);

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

Upvotes: 2

AlisonV2
AlisonV2

Reputation: 36

First, I'm a Miss, not Sir 😝

Well here is a verbose example, this is not the best practices, but I think it's the easier way to understand.


// First create an empty array to store your results. 
let expectedResults = [];

// Loop through you tree object
for (let i in tree) {
  
// create empty object for each tree object
  let treeObj = {};

  // get the name of the object
  const objName = tree[i]['name'];

  // assign it as a key and set it's value as object
  treeObj[objName] = {};

  // get the children
  const objChild = tree[i]['child'];

  // loop through the children
  for (let j in objChild) {

    // get the name
    const childName = objChild[j].name;

    // check the type and assign either as object either as array
    if (objChild[j].type === 'object') {
      treeObj[objName][childName] = {};
    }

    if (objChild[j].type === 'array') {
      treeObj[objName][childName] = [];

      const childArr = objChild[j].child;

      for (let k in childArr) {
        if (childArr[k].type === 'object') {
          treeObj[objName][childName].push({
              name: childArr[k].name
          });
        }

        if (childArr[k].type === 'array') {
          // and so on
        }
      }
    }
  }

  expectedResults.push(treeObj);
}

Upvotes: 0

AlisonV2
AlisonV2

Reputation: 36

  1. Parse your json data

https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse

  1. Map the data to fit your needs

https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Global_Objects/Array/map

(or loop through it, whatever works for you)

Upvotes: 0

Related Questions