Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2014-01-22
3
Medium Priority
?
6,490 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 44

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 44

Accepted Solution

by:
Chris Stanyon earned 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one 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…
Suggested Courses

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