Created
April 23, 2025 22:12
-
-
Save jhauga/722b61fdb8ce5210f74cbbfd3c272ffc to your computer and use it in GitHub Desktop.
NodeJS Gist - Check that a property with an array of objects has property with a specified value, and output to the terminal a specified adjacent property value.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ************************************************************* // | |
| // ************************ GITHUB GIST ************************ // | |
| // ************************************************************* // | |
| // checkJSONArrayOfObjects.js | |
| // NodeJS Gist - Check that a property with an array of objects has property with a specified value, and output to the terminal a specified adjacent property value. | |
| // | |
| // LICENSE | |
| // ******* | |
| // The code in this gist is public domain. | |
| // Licensed as Public Domain CC0 | |
| /////////////////////////////////////////////////////////////////// | |
| // NODEJS GIST - starting point gist | |
| // ************************************************************* | |
| // checkJSONArrayOfObjects.js | |
| // Check that a property with an array of objects has property with a specified value, | |
| // and output to the terminal a specified adjacent property value. | |
| var fs = require("fs"); | |
| // Declare global variables. | |
| var parOneCheckJSONArrayOfObjects, parTwoCheckJSONArrayOfObjects, | |
| parThreeCheckJSONArrayOfObjects, parFourCheckJSONArrayOfObjects, | |
| parFiveCheckJSONArrayOfObjects; | |
| // Define by parameter or use a default. | |
| if (process.argv[2]) { | |
| parOneCheckJSONArrayOfObjects = process.argv[2]; // full path to json file being checked | |
| } else { | |
| parOneCheckJSONArrayOfObjects = "data-input.json"; | |
| } | |
| // Property | |
| if (process.argv[3]) { | |
| parTwoCheckJSONArrayOfObjects = process.argv[3]; // property to check for | |
| } else { | |
| parTwoCheckJSONArrayOfObjects = "statutoryPolicyInputs"; | |
| } | |
| // Nested property | |
| if (process.argv[4]) { | |
| parThreeCheckJSONArrayOfObjects = process.argv[4]; // property to check for | |
| } else { | |
| parThreeCheckJSONArrayOfObjects = "value"; | |
| } | |
| // Property value | |
| if (process.argv[5]) { | |
| parFourCheckJSONArrayOfObjects = process.argv[5]; // value to check for | |
| } else { | |
| parFourCheckJSONArrayOfObjects = "SUBJECT"; | |
| } | |
| // Property whose value is output to terminal. | |
| if (process.argv[6]) { | |
| parFiveCheckJSONArrayOfObjects = process.argv[6]; // property to check for | |
| } else { | |
| parFiveCheckJSONArrayOfObjects = "id"; | |
| } | |
| // Format file to be read as JSON and add clarity with variable naming. | |
| var checkFile = fs.readFileSync(parOneCheckJSONArrayOfObjects); | |
| var fileJSON = JSON.parse(checkFile); | |
| var propCheck = parTwoCheckJSONArrayOfObjects; | |
| // Turn off by default. | |
| var checkReturn = 0; | |
| // Define as not found as essentially that is current status. | |
| var propertyValue = "not found"; | |
| // Recurse function. | |
| function recurseCheckJSONArrayOfObjects(cur) { | |
| for (i in cur) { // check each property | |
| if (checkReturn == 2) { // already found | |
| // do nothing | |
| continue; | |
| } else { | |
| if (typeof cur[i] == "object") { // check if object | |
| if (i == propCheck) { // is this the property | |
| if (checkReturn == 0) { // check recur nest status | |
| checkReturn = 1; // it is the propery | |
| propCheck = parThreeCheckJSONArrayOfObjects; // check for nested property | |
| recurseCheckJSONArrayOfObjects(cur[i]); // recurse | |
| } else { | |
| if (cur[i] == parFourCheckJSONArrayOfObjects) { | |
| propertyValue = "pass"; // Found value, but is not string | |
| } else { | |
| recurseCheckJSONArrayOfObjects(cur[i]); // recurse | |
| } | |
| } | |
| } else { // property not found check the next one | |
| recurseCheckJSONArrayOfObjects(cur[i]); // check the next property in iteration | |
| } | |
| } else { // check this property | |
| if (i == propCheck) { // is this the property | |
| if (checkReturn == 0) { // check recur nest status | |
| // illustration purposes | |
| let skip; // do nothing - it is a match but cannot be traversed | |
| } else { | |
| if (cur[i] == parFourCheckJSONArrayOfObjects) { | |
| propertyValue = // Get value of adjacent property | |
| cur[parFiveCheckJSONArrayOfObjects]; // this is the value being searched for | |
| checkReturn = 2; // found it -- done | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } | |
| // Call function to check if property in file. | |
| recurseCheckJSONArrayOfObjects(fileJSON); | |
| // Was the property found? | |
| console.log(propertyValue); // output results | |
| // PRACTICAL USE: | |
| // > node checkJSONArrayOfObjects.js "data.json" "policy" "id" "follower" "rate" | |
| // - Use data.json and check for a property - |policy", and if property is object | |
| // recure; looking for property "id" with value "Tax", and get adjacent property | |
| // "rate" value. | |
| // # TERMINAL OUTPUT - see sample data below: | |
| // # not-applied | |
| // | |
| // SAMPLE DATA: | |
| /****************************************************************************************** | |
| // data.json - contents below | |
| { | |
| "typeCode": { | |
| "code": "net" | |
| }, | |
| "policy": [ | |
| { | |
| "id": "member", | |
| "name": "status", | |
| "rate": "applied", | |
| "type": "string" | |
| }, | |
| { | |
| "id": "follower", | |
| "name": "status", | |
| "rate": "not-applied", | |
| "type": "string" | |
| }, etc... | |
| ], | |
| "likes": { | |
| "videos": [ | |
| { | |
| "title": "sample text", | |
| "description": "sample text with more words.", | |
| "metadata": { | |
| "name": "some", | |
| "code": "US" | |
| }, | |
| "count": { | |
| "likes": 10, | |
| "comments": 2 | |
| } | |
| }, etc... | |
| ], | |
| "shorts": [ | |
| { | |
| "title": "sample text", | |
| "description": "sample text with more words.", | |
| "metadata": { | |
| "name": "some", | |
| "code": "US" | |
| }, | |
| "count": { | |
| "likes": 10, | |
| "comments": 2 | |
| } | |
| }, etc... | |
| ] | |
| }, | |
| "profileCreated": "2020-03-01", | |
| "profileDetails": { | |
| "userName": "some_name", | |
| "userDescription": "About some name." | |
| }, | |
| "profileData": [ | |
| { | |
| "likes": 201, | |
| "comments": 47, | |
| "membership": "monthly", | |
| "external": [ | |
| { | |
| "followers": 1010, | |
| "members": 404, | |
| "following": 348 | |
| } | |
| ] | |
| } | |
| ], | |
| "additional": { | |
| "widgets": [] | |
| }, | |
| "tickets": [] | |
| } | |
| ******************************************************************************************* | |
| ******************************************************************************************/ | |
| // This gist is meant to work as a starting point. | |
| // Modifications should be made. Use as needed and/or see fit. | |
| // ************************************************************* | |
| // ************************************************************* |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment