1

I am trying to convert json object into array of path string but not getting expected result. Don't know might be other way to achieve the same but tried using for loop and lodash isObject and isEmpty for validate object like below.

Input Json:

    const json = {
  "info": {
    "account": {}
  },
  "info2": {
    "address": {
      "mobile": {
        "phone": null
      }
    },
    "contact": {
      "first": {}
    }
  }
}

javascript code:

var pathArray = this.convertJsonToArrayOfPathString(json);
    console.log(pathArray);
convertJsonToArrayOfPathString(json, pathArray = [], path = '') {
    const keys = Object.keys(json);
    for (let i = 0; i < keys.length; i++) {
      const key = keys[i];
      path = path ? path + '.' + key : key;

      if (_.isObject(json[key]) && !_.isEmpty(json[key])) {
        this.convertJsonToArrayOfPathString(json[key], pathArray, path);
      }
      else {
        pathArray.push(path)
        path = ''
      }
    }
    return pathArray;
  }

expected output:

[
      "info.account",
      "info2.address.mobile.phone",
      "info2.contact.first"
    ]

could you please any help. thanks.

3
  • 1
    There is no foreach in your code example. Are you posting the correct version? There is no need for isObject and isEmpty code if you use forEach. Commented May 24, 2022 at 11:57
  • @PeterKrebs updated question heading..but i am not sure whether using correct or not but what all i want expected output format for any input json. Commented May 24, 2022 at 12:03
  • 1
    Ok. BTW you forgot function before the function. Commented May 24, 2022 at 12:13

2 Answers 2

4

const json = {
  "info": {
    "account": {}
  },
  "info2": {
    "address": {
      "mobile": {
        "phone": null
      }
    },
    "contact": {
      "first": {}
    }
  }
};
var pathArray = convertJsonToArrayOfPathString(json);
console.log(pathArray);
function convertJsonToArrayOfPathString(json, pathArray = [], path = '') {
    const keys = Object.keys(json);
    for (let i = 0; i < keys.length; i++) {
      const key = keys[i];
      const newPath = path ? path + '.' + key : key;
      const value = json[key];
      if (typeof value === 'object' &&
            !Array.isArray(value) &&
            value !== null && Object.keys(value).length>0) {
        convertJsonToArrayOfPathString(json[key], pathArray, newPath);
      }
      else {
        pathArray.push(newPath);
      }
    }
    return pathArray;
}

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

Comments

1

const json = {
  "info": {
    "account": {}
  },
  "info2": {
    "address": {
      "mobile": {
        "phone": null
      }
    },
    "contact": {
      "first": {}
    }
  }
}

var pathArray = convertJsonToArrayOfPathString(json);
console.log(pathArray);


function convertJsonToArrayOfPathString(json, pathArray = [], path = '') 
{
    Object.keys(json).forEach( key =>
    {
      const p = path ? path + '.' + key : key;
      var test = !json[key] || Object.keys( json[key]).length < 1 || Array.isArray( json[key]) ||
                typeof  json[key] !== 'object';
      return  test ? 
              pathArray.push(p) :
              this.convertJsonToArrayOfPathString(json[key], pathArray, p);
    });
    
    return pathArray;
}

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.