Solved

Add row to JSON file

Posted on 2014-02-09
3
1,300 Views
Last Modified: 2014-02-12
I need to know how i can add a row to an existing JSON file, using PHP/Jquery.

My JSON file look's as folows (data.json):

{
"komp": [
{ "mail1":"xx@xx.xx" , "name1":"John" , "name2":"Dow" }
]
}

Open in new window

0
Comment
Question by:kbnewgear
  • 3
3 Comments
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39845539
Typically the new information would be added to an object, then the JSON string would be created from the object.  I'll see if I can show you a PHP example.
0
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39845553
Please see http://www.laprbass.com/RAY_temp_kbnewgear.php

<?php // RAY_temp_kbnewgear.php
error_reporting(E_ALL);

// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28360318.html
// REF http://www.json.org/

// THE EXISTING JSON STRING
$jso = <<<EOD
{
"komp": [
{ "mail1":"xx@xx.xx" , "name1":"John" , "name2":"Dow" }
]
}
EOD;

// MAKE AN OBJECT
$obj = json_decode($jso);
if (!$obj) trigger_error('JSON FAIL: ' .  json_last_error(), E_USER_ERROR);

// SHOW THE ORIGINAL OBJECT
echo '<pre>';
print_r($obj);
echo PHP_EOL;

// CREATE SOME NEW DATA TO APPEND TO THE komp ARRAY
$new = new StdClass;
$new->mail1 = 'Ray.P@Gmail.com';
$new->name1 = 'Ray';
$new->name2 = 'Paseur';

// INJECT SOME DATA INTO THE OBJECT
$obj->komp[] = $new;

// SHOW THE MODIFIED OBJECT
print_r($obj);

// RENDER THE NEW JSON STRING
echo json_encode($obj);

Open in new window

0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39853694
@kbnewgear: I see you are relatively new to Experts-Exchange and it appears that you may not understand the community norms and the grading guidelines.  The expected and default grade is "A" and anything less than an "A" should have an explanation of the deficiency.  Since you got a tested and working code sample that showed exactly how to do what you want, and since you didn't even post an acknowledgement, I'd like to ask for your explanation of the marked down grade now.

For your reference, the grading guidelines are shown here:
http://support.experts-exchange.com/customer/portal/articles/481419

Please let us hear back from you, thanks. ~Ray
0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

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.
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
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)

839 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