Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8100925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T22:51:49+00:00 2026-06-05T22:51:49+00:00

Suppose i have a json object friends as: { title: Friends link: /friends.json description:

  • 0

Suppose i have a json object friends as:

{
   title: Friends
   link: /friends.json
   description: "My friends"
   people: [
   {
   id: "1",
   name: "Matthew",
   img: "img/matthew.jpg"
   },
   {
   id: "2",
   name:"Matt",
   img: "img/matt.jpg"
   },
   {
   id: "3",
   name: "David",
   img: "img/dav.jpg"
   }
   ]
}

This is stored as a file friends.json

Now using javascript/jQuery, i need to create a new object good_friends.json and need to add values (DYNAMICALLY and one-by-one) to it from “people” field of friends.json using the “people.id”. How can i do that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T22:51:50+00:00Added an answer on June 5, 2026 at 10:51 pm

    You’re pretty much going to need a server-side process if you want to save your changes.

    You can load the JSON via ajax:

    $.ajax({
        url: "/path/to/friends.json",
        dataType: "json",
        success: function(data) {
            // Here, `data` will be the object resulting from deserializing the JSON
            // Store `data` somewhere useful, perhaps you might have a `friends`
            // variable declared somewhere; if so:
            friends = data;
        },
        error: function() {
           // Handle the error
        }
    });
    

    To add to the deserialized object, you just modify it in memory:

    friends.people.push({
        id: String(friends.people.length + 1),
        name: "John",
        img: "img/john.jpg"
    });
    

    Of course, those values will probably come from input fields or something, e.g.:

    function addPerson() {
        friends.people.push({
            id: String(friends.people.length + 1),
            name: $("#nameField").val(),
            img: $("#imgField").val()
        });
    }
    

    Now you have an in-memory copy of your data. To store it somewhere, you have to have a server-side process you can post it to. You’d probably serialize it before posting, e.g., via JSON.stringify or similar. If your browser doesn’t have JSON.stringify natively (most modern ones do, some older ones don’t), you can use Crockford’s.

    Or if this is just for your own use, you could display the stringified result in a text field and the use copy-and-paste to paste it into friends.json in a text editor.

    Here’s a complete example, which shows the JSON in a text area: Live copy | source

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset=utf-8 />
    <title>Test Page</title>
    <style>
      body {
        font-family: sans-serif;
      }
    </style>
    </head>
    <body>
      <label>Name:
        <input type="text" id="nameField">
      </label>
      <br><label>Img:
        <input type="text" id="imgField">
      </label>
      <br><input type="button" id="btnAdd" value="Add" disabled>
      <input type="button" id="btnShow" value="Show JSON" disabled>
      <br><div id="msg">&nbsp;</div>
      <hr>
      <textarea id="showField" rows="10" cols="60"></textarea>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
    <script>
    // Note that all of our script tags are at the end of the
    // document. This lets the page load as quickly as possible
    // and means we don't have to worry about whether the elements
    // have been created yet (they have, because the scripts are
    // below them).
    
    // Put all of our code inside a function so we don't
    // create globals
    (function($) {
        if (typeof JSON === "undefined") {
            // Load Crockford's json2.js
            // NOTE: You'd want to use your own copy, not a hotlink
            // to his github like this.
            var scr = document.createElement('script');
            scr.src = "https://raw.github.com/douglascrockford/JSON-js/master/json2.js";
            document.documentElement.appendChild(scr);
        }
    
        var friends; // Where our deserialized friends.json will go
    
        // Focus the first field
        $("#nameField").focus();
    
        // Load the JSON
        $.ajax({
            url: "http://jsbin.com/ojexuz",
            dataType: "json",
            success: function(data) {
                // Here, `data` will be the object resulting from deserializing the JSON
                // Store `data` somewhere useful, perhaps you might have a `friends`
                // variable declared somewhere; if so:
                friends = data;
    
                // Enable our buttons now that we have data
                $("input[type='button']").prop("disabled", "");
            },
            error: function() {
                // Handle the error
                alert("Error loading friends.json");
            }
        });
    
        // Handle clicks on the "Add" button
        $("#btnAdd").click(function() {
            var nameField = $("#nameField"),
                imgField  = $("#imgField"),
                name      = $.trim(nameField.val()),
                img       = $.trim(imgField.val());
            if (!name || !img) {
                alert("Please supply both name and image");
                return;
            }
            addPerson(name, img);
            $("#msg").text("Added '" + name + "'");
            nameField.focus();
        });
    
        // An "add this person" function
        function addPerson(name, img) {
            friends.people.push({
                id:   String(friends.people.length + 1),
                name: name,
                img:  img
            });
        }
    
        // Handle clicks on the "Show" button
        $("#btnShow").click(function() {
            $("#showField").val(JSON.stringify(friends));
        });
    
    })(jQuery);
    </script>
    </body>
    </html>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a json object that looks like: { id: 1, name: john
Suppose I have a url like: http://example.com/get-users which returns a JSON object of all
Let's suppose that we have the following JSON Object that describes a Person: {
Suppose you have some complex JSON like: {A : valA, B : { C
I have a huge JSON file to parse in my Android app. I suppose
Suppose I have a static method of my class that returns an object of
Suppose we have the name written in any none-latin letters - languages, like Arabic,
I have a JSON object like such: var list = {'name1' : {'element1': 'value1'},
I have in a json object which stores a JavaScript say var jsonObj =
I have a JSON structure; { books: [ {id: book1, title: Book One}, {id:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.