0

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: {}
    }
  },
]
2
  • Is tree always an array? Or always an array with single object in it? Commented Apr 23, 2022 at 7:38
  • Yes tree will be always an array of objects, Commented Apr 23, 2022 at 7:41

3 Answers 3

2

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; }

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

Comments

0
  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)

1 Comment

Hi Sir, Is it possible for you to provide an example ??
0

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);
}

Comments

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.