Solved

how to add/modify Json data(file) using javascript/jquery/ajax?

Posted on 2014-01-22
3
5,353 Views
Last Modified: 2014-01-24
Hi,

Is there a way to modify the json data using javascript/jquery/ajax?

Q1)
I need to add additional properties to the json data.

ex)
[
{ 'text': 'john doe', 'age':'29', 'id':'123', 'uri': 'xxxxx'},
{ 'text': 'jane doe', 'age':'22', 'id':'122', 'uri': 'xxxxx'},
{ 'text': 'joe doe', 'age':'12', 'id':'121', 'uri': 'xxxxx'}
]

Open in new window


I need to add 'parent':'xxxx'

I get the json data from the web service call.

can you show me an example or tutorial how I can do this please?
I must be looking at wrong place and can't find any examples.
Thanks.
0
Comment
Question by:dkim18
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 39802726
If you just wan't to add the value to one of the records, then something like this:

var json = [
{ 'text': 'john doe', 'age':'29', 'id':'123', 'uri': 'xxxxx'},
{ 'text': 'jane doe', 'age':'22', 'id':'122', 'uri': 'xxxxx'},
{ 'text': 'joe doe', 'age':'12', 'id':'121', 'uri': 'xxxxx'}
]

json[1].parent = 'xxxx';

Open in new window

If you want to loop through all the records, then like this:

$(json).each(function(i,elem) {
	elem.parent = 'xxxx';
});

Open in new window

0
 

Author Comment

by:dkim18
ID: 39806376
$(json).each(function(i,elem) {

are i and elem the builtin(??) property name or did you make them up?

also is json variable a json object or array?
I get confused on that.
Thanks.
0
 
LVL 43

Accepted Solution

by:
Chris Stanyon earned 500 total points
ID: 39806484
The each() method takes a function with 2 arguments - the index of each element and the element itself. The index is the zero-based number of the element: 0 - first element, 1 - second element etc.).

I just used i and elem, but you can call them whatever you like, as long as you stick to it inside your each function. Sometimes it makes for more readable code if you use a descriptive variable:

$(json).each(function(index, person) {
    person.parent = 'xxxx';
}

Open in new window

In my code example, I assigned your JSON data to a variable called json as you didn't show us how you were getting that. In your case you have an array of objects...which you can access either by index, or by looping through.

Hope that makes sense :)
0

Featured Post

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Javascript object functions help 1 19
Timer on div 5 23
Table header must be on top 2 25
sort Multi-dimensional array 6 10
Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question