Solved

Add row to JSON file

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Set css in function 11 56
Code planning methods/tools? 5 58
Last 4 numbers of a variable 9 24
Angular - why isn't this table working? 20 26
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
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.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

752 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