Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

95
Views
Changing and/or merging two JSON Objects structures

Lets say we are given a following JSON:

{
    "a": {
        "title": {
            "text": "text a"
        }
    },
    "b": {
        "title": {
            "text": "text b"
        }
    }
}

How could I convert and/or merge this into the following structure:

{
    "title": {
        "text": {
            "a": "text a",
            "b": "text b"
        }
    }
}

or a different example:

let aObject = {
    "level1": {
        "level2": {
            "level3": "text a"
        }
    }
}
let bObject = {
    "level1": {
        "level2": {
            "level3": "text b"
        }
    }
}
let cObject = {
    "level1": {
        "level2": {
            "level3": "text c"
        }
    }
}

into

{
    "level1": {
        "level2": {
            "level3": {
                "aObject": "text a",
                "bObject": "text b",
                "cObject": "text c"
            }
        }
    }
}

It might be very easy as I am missing a keyword describing this feature, or it might be complicated to archive this. Any help is appreciated.

Working example, looking for simpler solution:

let object = {
    "a": {
        "title": {
            "text": "text a"
        }
    },
    "b": {
        "title": {
            "text": "text b"
        }
    }
}

function reduce(node, object, result = {}) {
    for (let key in object) {
        if (typeof result[key] === 'undefined') {
            result[key] = {}
        }

        if (typeof object[key] === 'string') {
            result[key][node] = object[key]

        } else {
            reduce(node, object[key], result[key])
        }
    }

    return result
}

let result = Object.entries(object).reduce((result, [key, value]) => {
    return reduce(key, value, result)
}, {})

console.log(result)

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Can do in this way.:)

let obj = {
  a: {
    title: {
      text: "text a"
    }
  },
  b: {
    title: {
      text: "text b"
    }
  }
};




let test = {};
test["title"] = {};
test.title["text"] = {};

Object.keys(obj).forEach((key) => {
  test.title.text[key] = `text ${key}`;
});

console.log(test);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!