Solved

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

Posted on 2014-01-22
3
4,935 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
  • 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Weekend adv creator 3 38
How to use embargo Date/time in php 29 37
Javascript to set controls visibility 5 34
Call a function within the ASP code 4 11
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 …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

770 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