Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Javascript search JSON for name return parent object

Using this JSON object I am trying to find the parent object of xpublisher:

{
 type: "object",
 title: "Scalars",
 properties: {
   stringProperty: {
     description: "Property name's description (type is string)",
     type: "string",
     examples: [
       "example",
       "sample",
     ],
     xpublisher: [
       "Greg",
       "Tom",
     ],
   },
   writeOnlyStringProperty: {
     description: "Notice this only appears in the request.",
     xpublisher: [
       "John",
       "Anna",
     ],
     type: "string",
     writeOnly: true,
     examples: [
       "example",
     ],
   },
   minLengthString: {
     xpublisher: [
       "Nancy",
       "Tim",
     ],
     description: "Property name's description (type is string)",
     type: "string",
     minLength: 4,
     examples: [
       "example",
     ],
   },
 },
 }

I would like to search for xpublisher and return the parent object that includes the xpublisher tag so the first one found would be:

stringProperty: {
    description: "Property name's description (type is string)",
    type: "string",
    examples: [
      "example",
      "sample",
    ],
    xpublisher: [
      "Greg",
      "Tom",
    ],
  },

I have tried a few different way including:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

   function getPublishers(obj, name) {
      let result = [];
      for (var key in obj) {
          let keyName = obj[key]
          console.log(keyName)
          if (obj.hasOwnProperty(key)) {
              if ("object" == typeof(obj[key])) {
                  getPublishers(obj[key], name);
              } else if (key == name) {
                  result.push(obj[key]);
              }
          }
      }
      return result;
  }

And I also tried to find the key name this way also.

function searchObject(obj, name) {
   let result = Object.keys(obj).find(k => obj[k][name]);
   return result;
}

The code seems to only find key numbers not actually the key names so I can then grab the object.

>Solution :

You can modify your function to keep track of the parent object when the desired property is found.

function findParentObject(obj, targetPropertyName, parent = null) {
  for (const key in obj) {
    if (obj.hasOwnProperty(key)) {
      if (key === targetPropertyName) {
        return parent;
      } else if (typeof obj[key] === 'object' && obj[key] !== null) {
        const result = findParentObject(obj[key], targetPropertyName, obj);
        if (result !== null) {
          return result;
        }
      }
    }
  }
  return null;
}

const yourJsonObject = {
  // ... your JSON object ...
};

const parentObject = findParentObject(yourJsonObject.properties, 'xpublisher');
console.log(parentObject);

You can call this function with the properties object from your JSON.

const parentObject = findParentObject(yourJsonObject.properties, 'xpublisher');
console.log(parentObject);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading