• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
projectsgeek

ProjectsGeek

Download Mini projects with Source Code, Java projects with Source Codes

  • Home
  • Java Projects
  • C++ Projects
  • VB Projects
  • PHP projects
  • .Net Projects
  • NodeJs Projects
  • Android Projects
    • Project Ideas
      • Final Year Project Ideas
      • JSP Projects
  • Assignment Codes
    • Fundamentals of Programming Language
    • Software Design Laboratory
    • Data Structure and Files Lab
    • Computer Graphics Lab
    • Object Oriented Programming Lab
    • Assembly Codes
  • School Projects
  • Forum

Convert JavaScript Array to JSON

February 13, 2017 by ProjectsGeek Leave a Comment

Convert JavaScript Array to JSON

JSON

JavaScript object notation is very flexible and easy to read format for data interchange. It is very light weight and easy to parse for machines. You can use JSON with java script and other programming languages for data exchange from one end to another one. It is widely used in rest services.

In JavaScript we can use  JSON.stringify to convert an java script array into a JSON formatted string. Below is the code for converting JavaScript Array to JSON and displaying it in alert box.

Source Code 

Type 1 – JSON serializes Array

var temp= Array()
temp[0] = "apple";
temp[1] = "boy";
temp[2] = "cat";
temp[3] = "gun";

alert( JSON.stringify(temp) );
console.log( JSON.stringify(temp) );

Output

["apple","boy","cat","gun"]

JavaScript Array to JSON

 

 

 

 

 

 

Type 2 – JSON serializes Object

Here we are creating a array in java script and filling it with random string values. Then we are using JSON.stringify function which is in build function to convert it to JSON format.

var temp= {}
temp[0] = "apple";
temp[1] = "boy";
temp[2] = "cat";
temp[3] = "gun";

alert( JSON.stringify(temp) );
console.log( JSON.stringify(temp) );

Output 

{"0":"apple","1":"boy","2":"cat","3":"gun"}

Convert JavaScript Array to JSON

 

 

 

 

 

 

Usage of this Function

We can use this functionality for converting java script array data to JSON format for using in AJAX web service calls right from your browser.

var temp= Array()
temp[0] = "apple";
temp[1] = "boy";
temp[2] = "cat";
temp[3] = "gun";

var xmlhttp = new XMLHttpRequest(); 
xmlhttp.open("POST", "/json-handler");
xmlhttp.setRequestHeader("Content-Type", "application/json");
xmlhttp.send(JSON.stringify(temp));

Reference 

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

Other Projects to Try:

  1. Search Result
  2. TestPage
  3. To Perform various String Operation in Java
  4. Difference between VAR and LET Angular 2
  5. How to convert Binary to Gray conversion

Filed Under: JavaScript, Uncategorized Tagged With: JavaScript, JSON

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Tags

.Net Projects Download Android Project Ideas Android Projects Angular 2 Assembly Codes C # Projects C & C++ Projects C++ Projects Class Diagrams Computer Graphics Database Project Data Mining Projects DataScience Projects Datastructure Assignments Download Visual Basic Projects Electronics project Hadoop Projects Installation Guides Internet of Things Project IOS Projects Java Java Interview Questions Java Projects JavaScript JavaScript Projects java tutorial JSON JSP Projects Mechanical Projects Mongodb Networking Projects Node JS Projects OS Problems php Projects Placement Papers Project Ideas Python Projects seminar and presentation Struts

Search this Website


Footer

Download Java Project
Download Visual Basic Projects
Download .Net Projects
Download VB Projects
Download C++ Projects
Download NodeJs Projects
Download School Projects
Download School Projects
Ask Questions - Forum
Latest Projects Ideas
Assembly Codes
Datastructure Assignments
Computer Graphics Lab
Operating system Lab
australia-and-India-flag
  • Home
  • About me
  • Contact Form
  • Submit Your Work
  • Site Map
  • Privacy Policy