Obtaining a complete JSON object of a thing passed to a plugin: How to obtain all property values from a thing recursively?

Is it possible to obtain the value of properties of a thing passed to a server-side plugin, INCLUDING the value of all things referenced by properties of the thing?

For example, where we have a Thing A with a property of 'List of Thing B’s, then this code below correctly identifies all the values of all the properties that are directly linked to Thing A, but for all the related records or options, it only returns the value of the unique ids of the Thing B’s. All of the properties of Thing B are simply returned as null.

It seems as though the data fetch is done correctly for Thing A, but that there is no data fetch for any of the Things referenced by the properties of Thing A - only the properties are listed.

Am I missing a step to ‘fetch’ the sub-Things?

properties = { thing: Any type of data }

The object returned by your function should have this format.

{ thingasjson: String hashofjson: String }

function(properties, context) {

    function getPropertiesOf(thing) {
        let thingProperties;
        
        if (thing && thing['list_api']) { // This is a list, so iterate through it and get each element
            
            thingProperties=[]  // This is an array
            let list=thing.get(0,thing.length()) // Fetch all the elements of the list
            list.forEach(item=> {
                thingProperties.push(getPropertiesOf(item))  // For each item, fetch its value
            })           
            
        } else if (thing && thing['single_api']) { // This is a sub-thing, so get each property in turn
            
            let thingPropertiesList=thing.listProperties()  // Fetch the list of all the properties of the thing
            thingProperties={}  // This is an object
            thingPropertiesList.forEach(property=> {  // For each property, 'get' it, and then work out its value by recursively investigating it 
                let n=thing.get(property)  // Fetch its value
               	thingProperties[property]=getPropertiesOf(n) // Calculate what it is and assign to the thing object
            })            
            
        } else { // This is a specific value (or a null value), so just return it
            
            thingProperties=thing;  // A base value type, e.g. a string, number or boolean
            
        }
        
        return thingProperties;
                           
    }
                           
    const thingObject=getPropertiesOf(properties.thing) // Create an object containing the thing and all its properties, recursively

    var thingasjson=JSON.stringify(thingObject); // Force the object into a String version
    
	var hashofjson=require("crypto")
  		.createHash("sha256")
  		.update(thingasjson)
  		.digest("hex");
       
    return {
        thingasjson: thingasjson,
        hashofjson: hashofjson
    };     
}
1 Like

I should perhaps add that at first I attempted this without using the recursive function; we simply used ‘get’ on the all the fields, and then needed the data within the linked Things, but kept getting null values, so we built this recursive function to more generally understand the issue.