Solved

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

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

Expert Comment

by:Chris Stanyon
Comment Utility
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
Comment Utility
$(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 42

Accepted Solution

by:
Chris Stanyon earned 500 total points
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
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…

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now