Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

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

What is a Denial of Service (DoS)?

A DoS is a malicious attempt to prevent the normal operation of a computer system. You may frequently see the terms 'DDoS' (Distributed Denial of Service) and 'DoS' used interchangeably, but there are some subtle differences.

Question has a verified solution.

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

Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to dynamically set the form action using jQuery.
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)
Suggested Courses

722 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