Solved

Add row to JSON file

Posted on 2014-02-09
3
1,368 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
[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
  • 3
3 Comments
 
LVL 110

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 110

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 110

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

Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
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…

628 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