0

I am new to Nodejs and JSON manipulations. I have a jSON that looks like

"ent": [
        {
            "employee": [
                {
                    "emp_name": "",
                    "column": "employee",
                    "emp_id": 123456,
                    "first name": "Joe",
                    "last name": "Bloggs",
                    "email": "",
                    "ldapid":
                } ,
                 {
                    "emp_name": "",
                    "column": "employee",
                    "emp_id": 123456,
                    "first name": "Foo",
                    "last name": "Bars",
                    "email": "",
                    "ldapid":
                }
                
            ]
        }
        
        ]

I need to fill the email, ldapid and emp_name based on the firstname and last name

The desired output is

"ent": [
        {
            "employee": [
                {
                    "emp_name": "Joe Bloggs",
                    "column": "employee",
                    "emp_id": 123456,
                    "first name": "Joe",
                    "last name": "Bloggs",
                    "email": "[email protected]",
                    "ldapid": "jbloggs"
                } ,
                 {
                    "emp_name": "Foo Bars",
                    "column": "employee",
                    "emp_id": 567891,
                    "first name": "Foo",
                    "last name": "Bars",
                    "email": "[email protected]",
                    "ldapid": "fbars"
                }
                
            ]
        }
        
        ]

Since I am super new to the nodeJS world , I am making some initial steps to get to where I want..

The following is what I have done..

EDITED my POST

Hi All, Thanks for all your responses. I was hoping to get an answer that did something similar to the below. this may not be a code with best practices, but does what I want, may be experts in this group can make it better.

const fs = require('fs');

/** Method to start  
 * 
 *  
 */
const main = async () => {

    const myJSONObject = require('./people.json');
    try {
        for (var i = 0; i < myJSONObject.entities.length; i++) {
            var entity = myJSONObject.entities[i];

            if (entity.People) {
                for (var j = 0; j < entity.People.length; j++) {
                    var people = entity.People[j];
                    var fn = people["first name"];
                    var ln = people["last name"];
                    var email = `${fn.substring(0, 3)}${ln.substring(0, 5)}@mycompmail.com`;
                    var ldapid = `${fn.substring(0, 3)}${ln.substring(0, 5)}`;

                
                    myJSONObject.entities[i].People[j]["email"] = email.toLowerCase();
                    myJSONObject.entities[i].People[j]["ldap id"] = ldapid.toLowerCase();
                    myJSONObject.entities[i].People[j]["preferred first name"] = fn;
                    myJSONObject.entities[i].People[j]["preferred last name"] = ln;
                   
                    //  console.log(`${fn}.${ln}`)

                }

            }

        }
        fs.writeFileSync('./new_people.json', JSON.stringify(myJSONObject, 0, 4));
    }
    catch (error) {
        console.log(error);
    }

};

(async () => {
    await main();
})();

Any help in this is highly appreciated. Vakichak

5
  • 1
    Hm, have you tried something? Show us your code, please. We are not free-freelancers xD Commented Sep 27, 2020 at 19:58
  • What is your problem? Finding the correct element to update? Calculating the correct email address / id? Setting the values? Commented Sep 27, 2020 at 20:16
  • My problem is iterating through each of the employee object.. Once in the employee object , I am fine to do the rest. I started to write my code const myJSONObject = require('./people.json'); for(const i = 0; i < myJSONObject.entities.length; i++) { const entity = myJSONObject.entities[i]; // I know here that I have the first employee obj.. // I need to iterate through the children and get manipulate // I need to then create a new JSON output which has the changed values } Commented Sep 27, 2020 at 20:30
  • I edited my original post to show what I have done Commented Sep 27, 2020 at 20:37
  • so... ent (or entities?) is an array in which an item (or more?) may have the form {employee: [{object},{object} ]} (therefore employee is an array of employees?) Commented Sep 27, 2020 at 22:01

1 Answer 1

0

From your code snipped I assume, that the JSON is a string in a file.

So the first step you need to do is to import the file contents into a variable. You can do that with fs.readFileSync(). Now you have the string in a variable.

Next you need to do is to convert the string into an object. You can do that with JSON.parse(). Now you have an object that you can manipulate.

To write it the object back into a file, you can use JSON.stringify() to make it a string again and then fs.writeFileSync() to write it to the file.

Full script:

const jsonString = fs.readFileSync('./people.json')
const jsonObject = JSON.parse(jsonString)

// do stuff with jsonObject that is written into newJsonObject

const newJsonString = JSON.stringify(newJsonObject)
fs.writeFileSync('./new_people.json', newJsonObject)

Note: there's also async functions for writing and reading files. The sync functions are okay if you load a config or something like this at the beginning of a script. If you read/write many files during runtime, you should use the async functions.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.