In database JSON data is stored in below way:
{"name":"abc","score":5}
Now everytime I want to add json object with different name and score to same json data as :
{"name":"abc","score":5},{"name":"efg","score":8},{"name":"xyz","score":6}
Right now I have tried this code
$database_data = $row['JSONstring']; //fetched json string from database
$json_decode_data = json_decode($database);
array_push($json_decode_data, $new_json_string); //data to append to database data
$updated_json_data = json_encode($json_decode_data); // at last encode the update json data
But this don't work and throws a warning : Parameter 1 is an object.
How do solve it ?
Now with all the answer and comments given here I got the required answer. So I appreciate all your help.
In my question starting, the first JSON String format is invalid:
{"name":"abc","score":5}
Instead of this, it would be :
[{"name":"abc","score":5}]
Required JSON data's format would be:
[{"name":"abc","score":5},{"name":"efg","score":8},{"name":"xyz","score":6}]
And this is achieved by array_merge() function as follows:
Code to append json object to json object string
//fetched json string from database
ie.[{"name":"abc","score":5}]
$database_data = $row['JSONstring'];
//decode both string the database data string and the new one which is to be merged
$json_decode_database_data = json_decode($database_data, true);
$json_decode_new_data = json_decode($database_data, true);
//merge both the decoded array by array_merge()
$required_json_data = array_merge($json_decode_data, $new_json_string);
//at last encode required json data
$updated_json_data = json_encode($required_json_data);
// You will get required json data
[{"name":"abc","score":5},{"name":"efg","score":8},{"name":"xyz","score":6}]
Related
I have a json file:
$json='[{"Email":"myemail1#domain.com","Name":"company 1","Tel1":"xx-xx-xx","Adresse":"XXXXXX"},{"Email":"myemail2#domain.com","Name":"Company 2","Tel1":"xx-xx-xx","Adresse":"XXXXXX"}]';
and my forms post data in variable
vars="fname"=>"Ameur","lname"=>"KHIL"
"fname"=>"Marak","lname"=>"Cristo"
and I like to insert in between my json content with variable to get the final json like this:
$result='[{"Email":"myemail1#domain.com","Name":"company 1","vars":{"fname":"Ameur","lname":"KHIL","Tel1":"xx-xx-xx","Adresse":"XXXXXX"}},{"Email":"myemail2#domain.com","Name":"Company 2","vars":{"fname":"Marak","lname":"Cristo","Tel1":"xx-xx-xx","Adresse":"XXXXXX"}}]';
For this purpose you can use json_decode() to parse the JSON-String to a PHP-Object. Then you just set a new value vars to the given form values.
Parsing the JSON-String
$json = json_decode('[{"Email":"myemail1#domain.com","Name":"company 1","Tel1":"xx-xx-xx","Adresse":"XXXXXX"},{"Email":"myemail2#domain.com","Name":"Company 2","Tel1":"xx-xx-xx","Adresse":"XXXXXX"}]');
Adding the new vars value and removing the additional ones. This is just for the first entry but you can do the same for the other entry or even iterate through the array for multiple entries
$json[0]->vars = ["fname" => "Marak", "lname" => "Cristo", "Tel1" => $json[0]->Tel1,"Adresse" => $json[0]->Adresse];
unset($json[0]->Tel1);
unset($json[0]->Adresse);
And getting your result in a JSON-String
$result = json_encode($json);
I am storing data in json format in mysql database table column like
table A
column-user_details
"{\"name\":\"sadasfsf\",\"phone\":\"7896521747\",\"address_1\":\"dvgsdsd\",\"state_name\":\"g\",\"city_name\":\"sdgds\",\"zip_code\":\"ghdfh\"}"
I am fetching data like
json_decode($variable, true);
<?php echo $variable['name'];?>
However I am getting error like
Illegal string offset 'name'
the first parameter of json_decode is not a parameter passing by reference, so if you want to get the json code as php array, you mast adding the result of json_decode into a variable.
<?php
$variable = "{\"name\":\"sadasfsf\",\"phone\":\"7896521747\",\"address_1\":\"dvgsdsd\",\"state_name\":\"g\",\"city_name\":\"sdgds\",\"zip_code\":\"ghdfh\"}";
$array = json_decode($variable, true);
echo $array['name'];
NB: json_decode() is a php code, so you must use it after <?php tag, not outside of it
I'm saving an array to the db but when I do the array is getting quotes around it from the start to end
e.g
"[{"id":"1","country":"New Zealand","shipping_rate":"1"},{"id":"2","country":"Australia","shipping_rate":"2"}]"
the array shouldn't be quoted at the start and the end when saving but this is happening. When I check my model on the shipping field that is an array I tried to trim it but it's says it can't because it says it's an array, so the problem is when it's being saved to the db for that field
The array should just look like this when being saved unquoted
[{"id":"1","country":"New Zealand","shipping_rate":"1"},{"id":"2","country":"Australia","shipping_rate":"2"}]
mysql doesn't save arrays, it saves strings. When saving an array it is by design that it serializes the array into a string.
If you are using MySql 5.7.8 or later you can use the JSON data type.
https://dev.mysql.com/doc/refman/5.7/en/json.html
You can't save an array in the mysql directly it will throws exception, you should convert it in a string(use json_encode()) and save it in mysql in either varachar/text type field.
Again when you get the data from db, convert it in an array(use json_decode()) and then use it.
u must convert json string to array :
<?php
$json_string= '[{"id":"1","country":"New Zealand","shipping_rate":"1"},{"id":"2","country":"Australia","shipping_rate":"2"}]';
$array = json_decode($json_string,true);
?>
then make a loop with foreach and enter a query in foreach :
<?php
foreach($array as $data){
$id = $data['id'];
$country = $data['country'];
$save= mysql_query("insert into your_database values("$id", "$country")";
}
?>
i home this help
I converted php array to json and I try to add another field and data with quotes removed.
this is json object generated
$data_string = json_encode($data);
It outputs this .
{"dateDebut":"36000000","dateFin":"45000000","periodeDebut":"1410818400","periodeFin":"1411596000","jours":"Thursday","role":{"idRole":"1"},"zone":{"idzone":"Z1E2"},"tag":{"id":"511651969251"},"typeNotification":{"typeNotif":"Alerte"}}
I tried this
$data_string['message']=1;
and it outputs this wrong object with the "1" in the beginning
1"dateDebut":"36000000","dateFin":"45000000","periodeDebut":"1410818400","periodeFin":"1411596000","jours":"Thursday","role":{"idRole":"1"},"zone":{"idzone":"Z1E2"},"tag":{"id":"511651969251"},"typeNotification":{"typeNotif":"Alerte"}}
even adding the field with quotes like this
$data_string['message']="1";
doesn't add the field message in the generated object json at all.
You cant add data to the json string, because its a string.
Add the data before you json encode it:
$data['message']=1;
$data_string = json_encode($data);
Or if the original php object $data is out of scope by this point, you must decode to php object, add data and then encode back to json:
$data = json_decode($data_string);
$data['message']=1;
$data_string = json_encode($data);
YOu can do this:
$data_array = json_decode($data_string);
$data_array['message'] = 1;
$data_string = json_encode($data_array);
The string $data_string should contain the new member message with value 1;
The data is retrieved from the database then encoded into JSON data, then passed into the view:
$json_data = json_encode(array('questions' => $x));
$search_results['search_results'] = $json_data;
$this->load->view('findquestions',$search_results);
which looks like:
{"questions":[{"question_title":"java"},{"question_title":"big java book"}]}
How can the "question_title"s then be presented as separate links?
Does json_decode need to be used, then what