Skip to content Skip to sidebar Skip to footer

How To Convert Json Object Structure To Dot Notation?

I've got a variable I'm storing that will dictate what fields to exclude from a query: excludeFields = { Contact: { Address: 0, Phone: 0 } } I need to convert this to

Solution 1:

This should be flexible enough for most needs:

functiondotNotate(obj,target,prefix) {
  target = target || {},
  prefix = prefix || "";

  Object.keys(obj).forEach(function(key) {
    if ( typeof(obj[key]) === "object" && obj[key] !== null ) {
      dotNotate(obj[key],target,prefix + key + ".");
    } else {
      return target[prefix + key] = obj[key];
    }
  });

  return target;
}

Run on your excludesFields variable like so:

dotNotate(excludeFields);

It returns the current structure:

{"Contact.Address":0,"Contact.Phone":0}

So you can even do, inline:

things.findOne({}, {fields: dotNotate(excludeFields) })

Or provide as a projection:

var projection = { "fields": {} };
dotNotate(excludeFields,projection.fields);
things.findOne({}, projection);

Works nicely at all depths and even with arrays in an essential way, unless you need operators like $push.

Solution 2:

I use a function pretty much similar to the accepted answer

functionconvertJsonToDot(obj, parent = [], keyValue = {}) {
      for (let key in obj) {
        let keyPath = [...parent, key];
        if (obj[key]!== null && typeof obj[key] === 'object') {
            Object.assign(keyValue, convertJsonToDot(obj[key], keyPath, keyValue));
        } else {
            keyValue[keyPath.join('.')] = obj[key];
        }
    }
    return keyValue;
}

Here, I do an additional check 'obj[key] !== null' because unfortunately null is also of type 'object'.

I actually wanted to add this a comment to the accepted answer but couldn't because of not enough reputation.

Solution 3:

var fields = {};
for (var k in excludeFields) {
  for (var p in excludeFields[k]) {
    fields[k + '.' + p] = excludeFields[k][p];
  }
}

Then:

things.findOne({}, {fields: fields})

Post a Comment for "How To Convert Json Object Structure To Dot Notation?"