3

UPDATE

All the solutions in this post answer my question, but I can mark only one as excepted answer...

I have this JSON result:

[
   { 
      "unsorted1":[7,7,10,3,3]
   },
   {
      "unsorted2":[8,9,3,10,6]
   }
]

I would like to make an array in JavaScript that has each the "unsorted1" and "unsorted2" as keys, the [7,7,10,3,3] and [8,9,3,10,6] must remain in JSON format. An object like That unsorted1: [7,7,10,3,3] and unsorted2: [8,9,3,10,6]

This is what I get in the browser using the console.log(JSON.parse(returnVar)); the returnVar parameter holds my JSON result from above.

enter image description here

I read a lot and tried a lot, but nothing comes close to what I want. Somehow I can't get it done.

I tried modifying this code with no success

var resultObj = {};
for (var key in returnVar) {
    if (returnVar.hasOwnProperty(key)) {
        var val = returnVar[key];
        console.log("val: " + val);
        var keyName = Object.keys(val);
        console.log("key Name: " + keyName);
        for (var keykey in val) {
            if (val.hasOwnProperty(keykey)) {
                var valval = val[keykey]
                resultObj[keyName] = JSON.stringify(valval);
            }
        }
    }
}

3 Answers 3

1

//if the Json result is an object - this is how you parse it

var objJson = [{
  "unsorted1": [7, 7, 10, 3, 3]
}, {
  "unsorted2": [8, 9, 3, 10, 6]
}];

function parse() {
  if (Array.isArray(objJson) === true) {
    objJson.map(function(i) {
      //alert(JSON.stringify(i));
      console.log(JSON.stringify(i));
    });
  };
}
parse();

//if your json result is a string - this is how you do it
var strJson = "[{\"unsorted1\": [7, 7, 10, 3, 3]}, {\"unsorted2\": [8, 9, 3, 10, 6]}]"; //dont worry about the escaped quotes here - this is a string that is represented here.. but it will work if your JSON result is a string.

function parseString() {
  var objJson = JSON.parse(strJson);
  if (Array.isArray(objJson) === true) {
    objJson.map(function(i) {
      //alert(JSON.stringify(i));
      console.log(JSON.stringify(i));
    });
  };
}
parseString();

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

7 Comments

My results doesn't have the backslash before and after the unsorted1 and unsorted2. I get this JSON result as stated at the beginning of the post from a c++ code.
Is there a way not to use the JSON.parse() function for a json result is a string?
What do you mean? Why wouldn't you use JSON.parse() - I mean what is restraining you from using it?
I have a google portable native client module (PNaCl) that creates the JSON string output, I actually wouldn't want to do the job twice for very big JSON arrays. My normal output will have between 1000000 and 2000000 numbers, if I parse them all and then encode some of them back to JSON in JavaScript, then I loose the advantage of using the PNaCl module for this task.
@TalG Am sorry, am not familiar with PNaCl or its purpose, nevertheless I do not understand what you mean by wouldn't want to do the job twice, if you were to parse the array for specific numbers and encode them then assemble them back to a JSON object, I am not sure how efficiently you can do it without the parse functionality.
|
1

you can try this code:

var response = [
  { 
    "unsorted1":[7,7,10,3,3]
  },
  {
    "unsorted2":[8,9,3,10,6]
  }
];

var customObject = response.reduce(function(result, item) {
  var keys = Object.keys(item);

  if(keys.length) {
    result[keys[0]] = item[keys];
  }

  return result;
}, {});

1 Comment

Your code did exactly what i wanted when i changed the line result[keys[0]] = item[keys]; to result[keys[0]] = JSON.stringify(item[keys]);
1

var a = [
    {
        "unsorted1": [7, 7, 10, 3, 3]
    },
    {
        "unsorted2": [8, 9, 3, 10, 6]
    }
]

var b = {}

a.forEach(function(x) {
    Object.keys(x).forEach(function(y) {
        b[y] = JSON.stringify(x[y])
    })
})

console.log(b);

2 Comments

This one was also perfect. Is there a way to do this without using the JSON.parse(a); first. The thing is that the JSON array is being sent from my c++ code as a string. Do i have to change my c++ code in order to send it as an array and not as a string?
@TalG : I have updated my answer based on your comment here. The first version is if the given object is JsonObject the next version is if your result is a string. Also you need update your question with relevant details as to how you obtain that resultant - you say C++ what exactly do you mean? is this server side code ? how is your so called C++ passing this resultant object or how are you calling it from your client

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.