0

I have data in this format. This is gamesparks data that is BaaS using for game development.

I am sending this data to the IOS person but he said he can not fetch this type of data so he told me to change the data

This is my actual data

{
  "Details": [{
    "5d4c2c28dcf224127a30457b": {
      "displayName": "ewqeqw"
    },
    "5d4c4699dcf224127a3045e0": {
      "displayName": "mmmmmmmmmm"
    }
  }]
}

and I need to change data in this format

{
  "Details": [{
      "ID": "5d499b0fdcf224127a303d61",
      "displayName": "qweqewq"
    },
    {
      "ID": "5d499b0fdcf224127a303d61",
      "displayName": "qweqewq"
    }
  ]
}

This is my code:

var group = Spark.getData().group;
var API = Spark.getGameDataService();
var all1 = new Array();
var entry = API.getItem("playerFriends", Spark.getPlayer().getPlayerId());
var friendsList = {};
if (entry.error()) {
  Spark.setScriptError("ERROR", error);
  Spark.exit();
} else {
  var data = entry.document().getData();
  if (group === "all") {
    for (var friendOBJ in data) {
      //Set details of player ID and display name in new friendsList 
      object
      friendsList[friendOBJ] = {};
      friendsList[friendOBJ].displayName = data[friendOBJ].displayName;
      friendsList[friendOBJ].playerId = data[friendOBJ].playerId;


    }
    all1.push(friendsList);
  } else {
    for (var friendOBJ in data) {
      if (data[friendOBJ].group === group && data[friendOBJ].status ===
        "accepted") {
        friendsList[friendOBJ] = {};
        friendsList[friendOBJ].displayName = data[friendOBJ].displayName;
      }
    }
  }
  Spark.setScriptData("Details", all1);
Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
Uzii
  • 1
  • 1

2 Answers2

0

Can you not just make a function to convert the data into the desired shape? Something like this should work:

function formatData(details) {
  var formattedDetails = [];
  for (var id in details) {
    formattedDetails.push({
      ID: id,
      displayName: details[id].displayName
    });
  }
  return formattedDetails;
}

var data = {
  "Details": [
    {
      "5d4c2c28dcf224127a30457b": {
        "displayName": "ewqeqw"
      },
      "5d4c4699dcf224127a3045e0": {
        "displayName": "mmmmmmmmmm"
      }
    }
  ]
};

var formattedData = formatData(data.Details[0])
Mickey
  • 570
  • 3
  • 11
  • Seems like this is creating the data in the incorrect format, then writing more code to put it into the correct format, when the OP could just create the data in the correct format... – Heretic Monkey Aug 08 '19 at 20:25
  • I was assuming the data was being returned by an API they couldn't control and that they needed to format before sending it to a third party. If they're building the API, then yeah, they should just have it deliver the correctly formatted data to begin with. – Mickey Aug 08 '19 at 20:30
0

this is the output you want

{
  "Details": [{
      "ID": "5d499b0fdcf224127a303d61",
      "displayName": "qweqewq"
    }
 }

and this is my code i am explaining each line with comment

        var count = 0;
        var tmp = { AcceptedFriendList: []};         //make object and inside empty array                           
        for (var friendOBJ in data) {  // retrieving data
                 if(data[friendOBJ].status === "accepted"){ // your condition
            var tempObj = {"displayName" :"","playerid": ""}; //this is format you want
            tempObj.displayName = data[friendOBJ].displayName; // putting data in spicify format object

            tempObj.playerid =  data[friendOBJ].ID;

            tmp.AcceptedFriendList[count] = tempObj; //assign object back to array

            count++; // iterate it so the next data come further.
        }}
Uzair
  • 714
  • 2
  • 6
  • 17