0

We get a non nested object

obj = {
        "your-email": "[email protected]",
        "prename": "Jane Doe",
        "service__1": "Google",
        "login__1": "[email protected]",
        "password__1": "PW1",
        "service__2": "Microsoft",
        "login__2": "[email protected]",
        "password__2": "PW12!",
        "service__3": "Stackoverflow",
        "login__3": "[email protected]",
        "password__3": "3PW2!",
        "DPA": "1"
    }

We would like to transform this object into following structure:

nestedOBJ =         
{
        "your-email": "[email protected]",
        "prename": "Jane Doe",
        "DPA": "1",
        "service1": 
                  {
                    "service__1": "Google",
                    "login__1": "[email protected]",
                    "password__1": "PW1"
                  },
        "service2": 
                  {
                    "service__2": "Microsoft",
                    "login__2": "[email protected]",
                    "password__2": "PW12!"
                  },
        "service3": 
                  {
                    "service__3": "mySQL",
                    "login__3": "[email protected]",
                    "password__3": "3PW2!"
                  },
        "service4": 
                  {
                    "...":"..."
                  }
}

The following code has been tried without success:

let res = {}
let length = Object.keys(obj).length -3;
for (let i = 0; i < length  ; i + 3){
console.log(steps.trigger.event.body[0]["service__"+i])
  let onestedOBJ = {
    ["creds"+i]:steps.trigger.event.body[0]["service__"+i],
    ["creds"+i]:steps.trigger.event.body[0]["login__"+i],
    ["creds"+i]:steps.trigger.event.body[0]["password__"+i]
  }  
console.log(nestedOBJ)
}

We didn't include the DPA, your-email and prename. Because the for loop didn't even work.

2
  • 1
    What you have there has nothing to do with JSON. Commented Feb 27, 2022 at 19:46
  • What is steps.trigger.event.body Commented Feb 27, 2022 at 19:48

2 Answers 2

1
const nested = {};

for (const [key, value] of Object.entries(obj)) {
    const [, num] = key.split("__");

    if (num) {
        const parentKey = `service${num}`;

        if (nested[parentKey] === undefined) {
            nested[parentKey] = {};
        }
        
        nested[parentKey][key] = value;
    } else {
        nested[key] = value;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Array#reduce is one way to go about it

let obj = {
  "your-email": "[email protected]",
  "prename": "Jane Doe",
  "service__1": "Google",
  "login__1": "[email protected]",
  "password__1": "PW1",
  "service__2": "Microsoft",
  "login__2": "[email protected]",
  "password__2": "PW12!",
  "service__3": "Stackoverflow",
  "login__3": "[email protected]",
  "password__3": "3PW2!",
  "DPA": "1"
}

let subitems = ["service", "login", "password"];
let nestedOBJ = Object.entries(obj).reduce((b, a) => {
  let keya = a[0].split("__");
  if (subitems.indexOf(keya[0]) > -1) {
    b[`service${keya[1]}`] = b[`service${keya[1]}`] || {}
    b[`service${keya[1]}`][keya[0]] = a[1];
  } else b[a[0]] = a[1];
  return b;
}, {});
console.log(nestedOBJ);

1 Comment

Thanks works! Any idea how to nest the services into another sub json?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.