4

I have example :

var data = [{"name":"eric","age":"24"},{"name":"goulding","age":"23"}]

I want to convert jso above to json like this result :

[{name:"eric",age:24},{name:"goulding",age:23}]

Please give me advice.

4
  • 1
    What you've provided isn't JSON, it's simply a JSO. Commented Sep 3, 2015 at 12:38
  • 2
    Haha JSO! I gonna use it!
    – David Haim
    Commented Sep 3, 2015 at 12:56
  • Please confirm that your data is actually just a JavaScript object, and not a JSON string. Also, please change the title of the question to something meaningful.
    – user663031
    Commented Sep 4, 2015 at 13:43
  • @torazaburo Thank you, that's what I mean.
    – Rickseven
    Commented May 7, 2017 at 10:51

2 Answers 2

2

You need to use JSON.parse with a reviver parameter:

var jsonString = '[{"name":"eric","age":"24"},{"name":"goulding","age":"23"}]';

// given a string value, returns the number representation
// if possible, else returns the original value
var reviver = function (key, value) {
    var number = Number(value);

    return number === number ? number : value;
};

// because the reviver parameter is provided,
// the parse process will call it for each key-value pair
// in order to determine the ultimate value in a set
var data = JSON.parse(jsonString, reviver);

When the reviver is called with reviver("name", "eric"), it returns "eric" because "eric" cannot be converted to a number. However when called with reviver("age", "24"), the number 24 is returned.

Meanwhile, as others already noted the literal [{"name":"eric","age":"24"},{"name":"goulding","age":"23"}] is not JSON, it is an array. But the string '[{"name":"eric","age":"24"},{"name":"goulding","age":"23"}]' represents a valid JSON formatted array object.

4
  • This might be a good solution if the OP was actually starting off with a JSON string, but there are no real indications that that is the ase.
    – user663031
    Commented Sep 4, 2015 at 13:41
  • Would there be any need to convert an object to an object? The question is not skillfully worded, but if one really put some consideration into it one could read How to convert JSON String... to Object JavaScript?. We all probably can infer the OP's intention.
    – Igwe Kalu
    Commented Sep 4, 2015 at 13:49
  • We can equally infer that he actually doesn't know what JSON is, and thinks a JS object is JSON like many other people. That is supported by the fact that he did not show a quoted string in his question. It also seems a bit hard to imagine that someone has never even heard of JSON.parse. It's more likely that he somehow wants to "convert" strings into numbers. Bottom line, we don't know until the OP wakes up and answers the clarifying comment.
    – user663031
    Commented Sep 4, 2015 at 14:51
  • Your comments are sound.
    – Igwe Kalu
    Commented Sep 4, 2015 at 15:11
0
let data = [{"name":"eric","age":"24"},{"name":"goulding","age":"23"}] 

This is JSON and in order to convert it to a JavaScript object (jso) we need to use parse. If we want to manipulate JSON, we convert JSON to JSO using JSON.parse.

let convertedToJSO = JSON.parse(data)
let data = [{name:"eric",age:24},{name:"goulding",age:23}]

And this is a JSO. If you want to print or save JSO, convert JSO to JSON using JSON.stringfy.

let convertedToJSO = JSON.stringfy(data)

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.