{"id":30418,"date":"2023-01-14T21:00:38","date_gmt":"2023-01-14T15:30:38","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-adding-data-dynamically-from-one-json-object-to-another\/"},"modified":"2023-01-14T21:00:38","modified_gmt":"2023-01-14T15:30:38","slug":"solved-adding-data-dynamically-from-one-json-object-to-another","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-adding-data-dynamically-from-one-json-object-to-another\/","title":{"rendered":"[Solved] Adding data dynamically from one json object to another"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-11078770\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"11078770\" data-parentid=\"11078630\" data-score=\"2\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>You&#8217;re pretty much going to need a server-side process if you want to save your changes.<\/p>\n<p>You can load the JSON via <code>ajax<\/code>:<\/p>\n<pre><code>$.ajax({\n    url: \"\/path\/to\/friends.json\",\n    dataType: \"json\",\n    success: function(data) {\n        \/\/ Here, `data` will be the object resulting from deserializing the JSON\n        \/\/ Store `data` somewhere useful, perhaps you might have a `friends`\n        \/\/ variable declared somewhere; if so:\n        friends = data;\n    },\n    error: function() {\n       \/\/ Handle the error\n    }\n});\n<\/code><\/pre>\n<p>To add to the deserialized object, you just modify it in memory:<\/p>\n<pre><code>friends.people.push({\n    id: String(friends.people.length + 1),\n    name: \"John\",\n    img: \"img\/john.jpg\"\n});\n<\/code><\/pre>\n<p>Of course, those values will probably come from input fields or something, e.g.:<\/p>\n<pre><code>function addPerson() {\n    friends.people.push({\n        id: String(friends.people.length + 1),\n        name: $(\"#nameField\").val(),\n        img: $(\"#imgField\").val()\n    });\n}\n<\/code><\/pre>\n<p>Now you have an in-memory copy of your data. To <em>store<\/em> it somewhere, you have to have a server-side process you can post it to. You&#8217;d probably serialize it before posting, e.g., via <code>JSON.stringify<\/code> or similar. If your browser doesn&#8217;t have <code>JSON.stringify<\/code> natively (most modern ones do, some older ones don&#8217;t), you can use <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/github.com\/douglascrockford\/JSON-js\/\">Crockford&#8217;s<\/a>.<\/p>\n<p>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 <code>friends.json<\/code> in a text editor.<\/p>\n<p>Here&#8217;s a complete example, which shows the JSON in a text area: <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/jsbin.com\/eruqev\">Live copy<\/a> | <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/jsbin.com\/eruqev\/edit#html\">source<\/a><\/p>\n<pre><code>&lt;!DOCTYPE html&gt;\n&lt;html&gt;\n&lt;head&gt;\n&lt;meta charset=utf-8 \/&gt;\n&lt;title&gt;Test Page&lt;\/title&gt;\n&lt;style&gt;\n  body {\n    font-family: sans-serif;\n  }\n&lt;\/style&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n  &lt;label&gt;Name:\n    &lt;input type=\"text\" id=\"nameField\"&gt;\n  &lt;\/label&gt;\n  &lt;br&gt;&lt;label&gt;Img:\n    &lt;input type=\"text\" id=\"imgField\"&gt;\n  &lt;\/label&gt;\n  &lt;br&gt;&lt;input type=\"button\" id=\"btnAdd\" value=\"Add\" disabled&gt;\n  &lt;input type=\"button\" id=\"btnShow\" value=\"Show JSON\" disabled&gt;\n  &lt;br&gt;&lt;div id=\"msg\"&gt;&amp;nbsp;&lt;\/div&gt;\n  &lt;hr&gt;\n  &lt;textarea id=\"showField\" rows=\"10\" cols=\"60\"&gt;&lt;\/textarea&gt;\n&lt;script src=\"http:\/\/ajax.googleapis.com\/ajax\/libs\/jquery\/1\/jquery.min.js\"&gt;&lt;\/script&gt;\n&lt;script&gt;\n\/\/ Note that all of our script tags are at the end of the\n\/\/ document. This lets the page load as quickly as possible\n\/\/ and means we don't have to worry about whether the elements\n\/\/ have been created yet (they have, because the scripts are\n\/\/ below them).\n\n\/\/ Put all of our code inside a function so we don't\n\/\/ create globals\n(function($) {\n    if (typeof JSON === \"undefined\") {\n        \/\/ Load Crockford's json2.js\n        \/\/ NOTE: You'd want to use your own copy, not a hotlink\n        \/\/ to his github like this.\n        var scr = document.createElement('script');\n        scr.src = \"https:\/\/raw.github.com\/douglascrockford\/JSON-js\/master\/json2.js\";\n        document.documentElement.appendChild(scr);\n    }\n\n    var friends; \/\/ Where our deserialized friends.json will go\n\n    \/\/ Focus the first field\n    $(\"#nameField\").focus();\n\n    \/\/ Load the JSON\n    $.ajax({\n        url: \"http:\/\/jsbin.com\/ojexuz\",\n        dataType: \"json\",\n        success: function(data) {\n            \/\/ Here, `data` will be the object resulting from deserializing the JSON\n            \/\/ Store `data` somewhere useful, perhaps you might have a `friends`\n            \/\/ variable declared somewhere; if so:\n            friends = data;\n\n            \/\/ Enable our buttons now that we have data\n            $(\"input[type=\"button\"]\").prop(\"disabled\", \"\");\n        },\n        error: function() {\n            \/\/ Handle the error\n            alert(\"Error loading friends.json\");\n        }\n    });\n\n    \/\/ Handle clicks on the \"Add\" button\n    $(\"#btnAdd\").click(function() {\n        var nameField = $(\"#nameField\"),\n            imgField  = $(\"#imgField\"),\n            name      = $.trim(nameField.val()),\n            img       = $.trim(imgField.val());\n        if (!name || !img) {\n            alert(\"Please supply both name and image\");\n            return;\n        }\n        addPerson(name, img);\n        $(\"#msg\").text(\"Added '\" + name + \"'\");\n        nameField.focus();\n    });\n\n    \/\/ An \"add this person\" function\n    function addPerson(name, img) {\n        friends.people.push({\n            id:   String(friends.people.length + 1),\n            name: name,\n            img:  img\n        });\n    }\n\n    \/\/ Handle clicks on the \"Show\" button\n    $(\"#btnShow\").click(function() {\n        $(\"#showField\").val(JSON.stringify(friends));\n    });\n\n})(jQuery);\n&lt;\/script&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n<\/code><\/pre>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">5<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Adding data dynamically from one json object to another <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] You&#8217;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: &#8220;\/path\/to\/friends.json&#8221;, dataType: &#8220;json&#8221;, success: function(data) { \/\/ Here, `data` will be the object resulting from deserializing the JSON \/\/ Store `data` somewhere useful, perhaps you might have a `friends` &#8230; <a title=\"[Solved] Adding data dynamically from one json object to another\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-adding-data-dynamically-from-one-json-object-to-another\/\" aria-label=\"More on [Solved] Adding data dynamically from one json object to another\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[2216,333,388,356],"class_list":["post-30418","post","type-post","status-publish","format-standard","hentry","category-solved","tag-dynamically-generated","tag-javascript","tag-jquery","tag-json"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Adding data dynamically from one json object to another - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-adding-data-dynamically-from-one-json-object-to-another\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Adding data dynamically from one json object to another - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] You&#8217;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: &quot;\/path\/to\/friends.json&quot;, dataType: &quot;json&quot;, success: function(data) { \/\/ Here, `data` will be the object resulting from deserializing the JSON \/\/ Store `data` somewhere useful, perhaps you might have a `friends` ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-adding-data-dynamically-from-one-json-object-to-another\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2023-01-14T15:30:38+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-adding-data-dynamically-from-one-json-object-to-another\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-adding-data-dynamically-from-one-json-object-to-another\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Adding data dynamically from one json object to another\",\"datePublished\":\"2023-01-14T15:30:38+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-adding-data-dynamically-from-one-json-object-to-another\/\"},\"wordCount\":176,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"dynamically-generated\",\"javascript\",\"jquery\",\"json\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-adding-data-dynamically-from-one-json-object-to-another\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-adding-data-dynamically-from-one-json-object-to-another\/\",\"name\":\"[Solved] Adding data dynamically from one json object to another - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2023-01-14T15:30:38+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-adding-data-dynamically-from-one-json-object-to-another\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-adding-data-dynamically-from-one-json-object-to-another\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-adding-data-dynamically-from-one-json-object-to-another\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Adding data dynamically from one json object to another\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Adding data dynamically from one json object to another - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-adding-data-dynamically-from-one-json-object-to-another\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Adding data dynamically from one json object to another - JassWeb","og_description":"[ad_1] You&#8217;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` ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-adding-data-dynamically-from-one-json-object-to-another\/","og_site_name":"JassWeb","article_published_time":"2023-01-14T15:30:38+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-adding-data-dynamically-from-one-json-object-to-another\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-adding-data-dynamically-from-one-json-object-to-another\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Adding data dynamically from one json object to another","datePublished":"2023-01-14T15:30:38+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-adding-data-dynamically-from-one-json-object-to-another\/"},"wordCount":176,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["dynamically-generated","javascript","jquery","json"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-adding-data-dynamically-from-one-json-object-to-another\/","url":"https:\/\/jassweb.com\/solved\/solved-adding-data-dynamically-from-one-json-object-to-another\/","name":"[Solved] Adding data dynamically from one json object to another - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2023-01-14T15:30:38+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-adding-data-dynamically-from-one-json-object-to-another\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-adding-data-dynamically-from-one-json-object-to-another\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-adding-data-dynamically-from-one-json-object-to-another\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Adding data dynamically from one json object to another"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/30418","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=30418"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/30418\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=30418"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=30418"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=30418"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}