Solved

Add row to JSON file

Posted on 2014-02-09
3
1,249 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 108

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 108

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 108

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now