0

Currently my ajax request is getting JSON in this format:

    {
        id: 1,
        teamname: "Chicago Blackhawks",
        league: NHL
        wins: 5
        losses: 10
        account_id: 3444,
    },
    {
        id: 2,
        teamname: "Chicago Bulls",
        league: NBA
        wins: 15
        losses: 2
        account_id: 3444,
    }

but to use an autocomplete jquery plugin, I need it in this format:

[
   { value: 'Chicago Blackhawks', data: { category: 'NHL' } },
   { value: 'Chicago Bulls', data: { category: 'NBA' } }
]

Basically I need to only get two fields and format it under value and data -> category instead of name and league. What is the best way to go about this?

1
  • 1
    JSON is just a notation. Parse it into a native JavaScript Object and then apply any transformations desired as normal Commented Dec 6, 2015 at 3:03

4 Answers 4

4

Your data contains some typos, but once you fix those you get:

var data  = [{
    id: 1,
    teamname: "Chicago Blackhawks",
    league: "NHL",
    wins: 5,
    losses: 10,
    account_id: 3444,
},
{
    id: 2,
    teamname: "Chicago Bulls",
    league: "NBA",
    wins: 15,
    losses: 2,
    account_id: 3444,
}];

From which you can extract your desired list:

var newData = data.map(function(d) {
  return {
    value: d.teamname,
    data: {category: d.league}
  };
});

newData[0];
//=> Object {value: "Chicago Blackhawks", data: "NHL"}

Ack. Thanks Salman for the correction.

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

2 Comments

@AndrewD What does your data list look like?
thanks I figured it out and this worked perfectly! I was using a made up data list for this question and I copied your answer which is why it was returning undefined values
1

In order to get the data key in your resulting objects to have object notation you should modify the code from @Hunan's answer just a bit, like so:

var newData = data.map(function(d) {
  return {
    value: d.teamname,
    data: {"category": d.league}
  };
});

1 Comment

Thanks man. I had overlooked the category. Updated my answer.
1

try this,

var jsonData  = [{
    id: 1,
    teamname: "Chicago Blackhawks",
    league: "NHL",
    wins: 5,
    losses: 10,
    account_id: 3444,
},
{
    id: 2,
    teamname: "Chicago Bulls",
    league: "NBA",
    wins: 15,
    losses: 2,
    account_id: 3444,
}];

function formatJson(jsonData){

  var rslt = [];
  for(var i = 0; i < jsonData.length; i++)
    rslt.push({ value: jsonData[i].teamname, data: { category: jsonData[i].league} });

   return rslt;
}

var mydata = formatJson(jsonData);

Comments

1

Assuming that your JSON is valid and @Anik1991 is correct. You can do the following:

var json = '{"teams":[{"id": 1,"teamname": "Chicago Blackhawks","league": "NHL","wins": 5,"losses": 10,"account_id": 3444},{"id": 2,"teamname": "Chicago Bulls","league": "NBA","wins": 15,"losses": 2,"account_id": 3444}]}';

// convert your JSON to an object so you can retrieve values you need
var obj = JSON.parse(json);

// create a new array to store your values in the desired format
var newObj = [];

//for each team in obj, push the values you need as an object
obj['teams'].forEach(function(team){
  newObj.push({
    value: team.teamname,
    data: {
      category: team.league
    }
  });
});

// Finally use JSON.stringify to convert newObj to JSON
console.log(JSON.stringify(newObj));

See https://jsfiddle.net/hsxgrom6/

Comments

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.