22

I would like to convert an array of JSON String to array of JSON object without looping through each item and parse it using JSON.parse.

Example:

var s=[
  '{"Select":"11", "PhotoCount":"12"}',
  '{"Select":"21", "PhotoCount":"22"}',
  '{"Select":"31", "PhotoCount":"32"}'];
4
  • 10
    That's already a JavaScript array of objects! Commented Apr 27, 2012 at 19:00
  • 3
    Please confirm that you actually have a JavaScript array of JavaScript objects (as you typed in your question). If so, please explain what you want (since the title of your question is entirely misleading if this is the case). Commented Apr 27, 2012 at 19:07
  • Yes it is a javascript array of json string objects Commented Apr 27, 2012 at 19:28
  • I've edited your question to match what you just said. Please ensure that you have used the correct terminology, that what you see in the question now matches what you have. Commented Apr 27, 2012 at 19:38

3 Answers 3

54

If you have a JS array of JSON objects:

var s=['{"Select":"11","PhotoCount":"12"}','{"Select":"21","PhotoCount":"22"}'];

and you want an array of objects:

// JavaScript array of JavaScript objects
var objs = s.map(JSON.parse);

// ...or for older browsers
var objs=[];
for (var i=s.length;i--;) objs[i]=JSON.parse(s[i]);

// ...or for maximum speed:
var objs = JSON.parse('['+s.join(',')+']');

See the speed tests for browser comparisons.


If you have a single JSON string representing an array of objects:

var s='[{"Select":"11","PhotoCount":"12"},{"Select":"21","PhotoCount":"22"}]';

and you want an array of objects:

// JavaScript array of JavaScript objects
var objs = JSON.parse(s);

If you have an array of objects:

// A JavaScript array of JavaScript objects
var s = [{"Select":"11", "PhotoCount":"12"},{"Select":"21", "PhotoCount":"22"}];

…and you want JSON representation for it, then:

// JSON string representing an array of objects
var json = JSON.stringify(s);

…or if you want a JavaScript array of JSON strings, then:

// JavaScript array of strings (that are each a JSON object)
var jsons = s.map(JSON.stringify);

// ...or for older browsers
var jsons=[];
for (var i=s.length;i--;) jsons[i]=JSON.stringify(s[i]);
Sign up to request clarification or add additional context in comments.

8 Comments

Or var objs = s.map(JSON.parse); for your first example.
@AlaaOsta Read again what I am supposing your data is. It is not clear what data you really have and what data you really want.
@amnotiam var objs = s.map(JSON.parse);No this won't work in IE8
You need IE8 support? Then instead use: var objs = []; for (var i=s.length;i--;) objs[i] = JSON.parse(s[i]);; see my edits
@Phrogz: I would do it from the beginning, I exposed this in my question that I dont wanna use this way because I have a huge data
|
7
var json = jQuery.parseJSON(s); //If you have jQuery.

Since the comment looks cluttered, please use the parse function after enclosing those square brackets inside the quotes.

var s=['{"Select":"11","PhotoCount":"12"}','{"Select":"21","PhotoCount":"22"}'];

Change the above code to

var s='[{"Select":"11","PhotoCount":"12"},{"Select":"21","PhotoCount":"22"}]';

Eg:

$(document).ready(function() {
    var s= '[{"Select":"11","PhotoCount":"12"},{"Select":"21","PhotoCount":"22"}]';

    s = jQuery.parseJSON(s);

    alert( s[0]["Select"] );
});

And then use the parse function. It'll surely work.

EDIT :Extremely sorry that I gave the wrong function name. it's jQuery.parseJSON

Jquery

The json api

Edit (30 April 2020):

Editing since I got an upvote for this answer. There's a browser native function available instead of JQuery (for nonJQuery users), JSON.parse("<json string here>")

4 Comments

what is the plugin or the js file for it?
Can you also include a link to the json_encode jQuery plugin? It definitely isn't a core method.
jQuery does however provide a jQuery.parseJSON() method.
var s=['{"Select":"11","PhotoCount":"12"}','{"Select":"21","PhotoCount":"22"}']; Try changing that to : var s='[{"Select":"11","PhotoCount":"12"}','{"Select":"21","PhotoCount":"22"}]'; and then use the parse function.
0

If you really have:

var s = ['{"Select":"11", "PhotoCount":"12"}','{"Select":"21", "PhotoCount":"22"}'];

then simply:

var objs = $.map(s, $.parseJSON);

Here's a demo.

4 Comments

@AlaaOsta: Sorry, I forgot the s, . Try again.
@AlaaOsta: Please note that it won't work on IE 8 or earlier, though.
@AlaaOsta: I meant the top-voted solution will not work in IE 8. This one will, as well as the other one by xFourtyFourx.

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.