Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8492867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:50:54+00:00 2026-06-10T22:50:54+00:00

From a service I receive a JSON object with key-value pairs, and I need

  • 0

From a service I receive a JSON object with key-value pairs, and I need to dynamically create objects from them, grouped by one column.

The JSON looks like this:

[
    { "Group" : "A", "Key" : "Name", "Value" : "John" },
    { "Group" : "A", "Key" : "Age",  "Value" : "30" },
    { "Group" : "A", "Key" : "City", "Value" : "London" },
    { "Group" : "B", "Key" : "Name", "Value" : "Hans" },
    { "Group" : "B", "Key" : "Age",  "Value" : "35" },
    { "Group" : "B", "Key" : "City", "Value" : "Berlin" },
    { "Group" : "C", "Key" : "Name", "Value" : "José" },
    { "Group" : "C", "Key" : "Age",  "Value" : "25" },
    { "Group" : "C", "Key" : "City", "Value" : "Madrid" }
]

I would need to transform it to the following array of objects:

[
    { Group : "A", Name : "John", Age : 30, City : "London" },
    { Group : "B", Name : "Hans", Age : 35, City : "Berlin" },
    { Group : "C", Name : "José", Age : 25, City : "Madrid" }
]

Each group can contain any number of key-value pairs.

Currently I have a working solution for this, but I don’t know if it’s optimal:

var items = [
    { "Group" : "A", "Key" : "Name", "Value" : "John" },
    { "Group" : "A", "Key" : "Age",  "Value" : "30" },
    { "Group" : "A", "Key" : "City", "Value" : "London" },
    { "Group" : "B", "Key" : "Name", "Value" : "Hans" },
    { "Group" : "B", "Key" : "Age",  "Value" : "35" },
    { "Group" : "B", "Key" : "City", "Value" : "Berlin" },
    { "Group" : "C", "Key" : "Name", "Value" : "José" },
    { "Group" : "C", "Key" : "Age",  "Value" : "25" },
    { "Group" : "C", "Key" : "City", "Value" : "Madrid" }
], item, record, hash = {}, results = [];

// Create a "hash" object to build up
for (var i = 0, len = items.length; i < len; i += 1) {
    item = items[i];

  if (!hash[item.Group]) {
    hash[item.Group] = {
      Group : item.Group
    };
  }
  hash[item.Group][item.Key] = item.Value;
}

// Push each item in the hash to the array
for (record in hash) {
  if(hash.hasOwnProperty(record)) {
    results.push(hash[record]);
  }
}

You can check the fiddle here: http://jsbin.com/ozizom/1/

Do you have a better solution for this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T22:50:56+00:00Added an answer on June 10, 2026 at 10:50 pm

    Assuming that the JSON records will always be sorted by Group, here is another approach:

    var json = [
        { "Group" : "A", "Key" : "Name", "Value" : "John" },
        { "Group" : "A", "Key" : "Age",  "Value" : "30" },
        { "Group" : "A", "Key" : "City", "Value" : "London" },
        { "Group" : "B", "Key" : "Name", "Value" : "Hans" },
        { "Group" : "B", "Key" : "Age",  "Value" : "35" },
        { "Group" : "B", "Key" : "City", "Value" : "Berlin" },
        { "Group" : "C", "Key" : "Name", "Value" : "José" },
        { "Group" : "C", "Key" : "Age",  "Value" : "25" },
        { "Group" : "C", "Key" : "City", "Value" : "Madrid" }
    ];
    
    var array = [];
    var previousGroup = null;
    
    for(var i=0; i<json.length; i++) {
        var group = json[i].Group;
        if(previousGroup != group) {
            array.push({Group: group});
            previousGroup = group;
        }
        array[array.length-1][json[i].Key] = json[i].Value;
    }
    

    Here is a working example.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using JSON to receive data from the web service , creating text
I'm using jqPlot and I need to turn this JSON which I receive from
I have a WCF service that receive query from a silverlight client and send
I am send data from one service to other service using parecable, when I
I have been having some issues retrieving JSON data from a WCF service application
Project Requirements: I need to build a web service that will receive chunks of
I have a web service that receives requests from users and returns some json.
I can successfully receive values from my web service so in that repect the
I have implemented a web service in PHP. This service takes name/value pairs as
Here's the scenario: I receive a JSON object comprising 140 events. Each event is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.