Edit json file with php - php

i have a json file structured this way:
{
"data": {
"Category": {
"Product1": [{
"Code": "abc"
}],
"Product2": [{
"Code": "cba"
}]
},
"Category2": {
"Product3": [{
"Code": "abcabc"
}],
"Product4": [{
"Code": "cbacba"
}]
},
"Category3": {
"Product5": [{
"Code": "abcabcabc"
}],
"Product6": [{
"Code": "cbacbacba"
}]
}
}
}
How can i change the category names with a php script?
for example if i have 2 variables received from a form, called "$oldcategory" (which will be one of the existing categories) and "$newcategory" how can it, open the json file, place "$newcategory" where "$oldcategory" is and re-save the file?
<?php
$oldcategory= $_POST['oldcategory'];
$newcategory= $_POST['newcategory'];
$jsonString = file_get_contents('data.json');
$data = json_decode($jsonString, true);
...
foreach($data["data"] as $key=>$value){
$new = $data["data"][$oldcategory];
$data["data"][$newcategory]= $new ;
}
unset($data["data"][$oldcategory]);
$newJsonString = json_encode($data);
file_put_contents('data.json', $newJsonString);
?>
in case i was changing the "Category2" i'd like the result to be:
{
"data": {
"Category": {
"Product1": [{
"Code": "abc"
}],
"Product2": [{
"Code": "cba"
}]
},
"New_Category2": {
"Product3": [{
"Code": "abcabc"
}],
"Product4": [{
"Code": "cbacba"
}]
},
"Category3": {
"Product5": [{
"Code": "abcabcabc"
}],
"Product6": [{
"Code": "cbacbacba"
}]
}
}
}
instead of this:
{
"data": {
"Category": {
"Product1": [{
"Code": "abc"
}],
"Product2": [{
"Code": "cba"
}]
},
"Category3": {
"Product5": [{
"Code": "abcabcabc"
}],
"Product6": [{
"Code": "cbacbacba"
}]
},
"New_Category2": {
"Product3": [{
"Code": "abcabc"
}],
"Product4": [{
"Code": "cbacba"
}]
}
}
}
This was a possible solution:
foreach($data["data"] as $key=>$value){
if ($key == $oldcategory){
$key = $newcategory;}
$new["data"][$key] = $value;
}
$newJsonString = json_encode($new);
file_put_contents('data.json', $newJsonString);

This ended up being the solution to my problem:
<?php
$oldcategory= $_POST['oldcategory'];
$newcategory= $_POST['newcategory'];
$jsonString = file_get_contents('data.json');
$data = json_decode($jsonString, true);
...
foreach($data["data"] as $key=>$value){
if ($key == $oldcategory){
$key = $newcategory;}
$new["data"][$key] = $value;
}
$newJsonString = json_encode($new);
file_put_contents('data.json', $newJsonString);
?>

You could use a conditional, and create a new array with the desired values. When you reach the $oldcategory in your loop, replace the key with the $newcategory.
$newArray = [];
foreach($data["data"] as $key=>$value){
if($key == $oldcategory) {
$newArray[$newcategory] = $value;
} else {
$newArray[$key] = $value;
}
}
$newJsonString = json_encode($newArray);

Related

Jotform: Parse returned data issue

I'm trying to parse data returned by the Jotform API.
I'm successfully echoing the data but I'm also getting unnecessary additional lines of text.
My PHP code is:
include "JotForm.php";
$jotformAPI = new JotForm("myapikey");
$submissions = $jotformAPI->getFormSubmissions("myformid");
var_dump($submissions );
foreach ($submissions as $data) {
$detail = $jotformAPI->getSubmission($data['id']);
foreach ($detail as $d) {
echo $d[1]['answer']['first'] . '<br>';
}
}
result of var_dump($submissions );
{
"responseCode": 200,
"message": "success",
"content": [{
"id": "237955080346633702",
"form_id": "31751954731962",
"ip": "123.123.123.123",
"created_at": "2013-06-25 03:38:00",
"updated_at": "2013-06-27 04:58:00",
"status": "ACTIVE",
"new": "1",
"answers": {
"1": {
"text": "Name",
"type":"control_fullname",
"answer": {
"first": "LeBron",
"last": "James"
},
"prettyFormat": "LeBron James"
},
"2": {
"text": "Your Message",
"type": "control_textarea",
"answer":"¡Ay, caramba!"
}
}],
}
And here is the result I'm getting:
1
0
0
0
C
0
LeBron
I had to repair your invalid json string...
Code: (Demo)
$json = '{
"responseCode": 200,
"message": "success",
"content": [{
"id": "237955080346633702",
"form_id": "31751954731962",
"ip": "123.123.123.123",
"created_at": "2013-06-25 03:38:00",
"updated_at": "2013-06-27 04:58:00",
"status": "ACTIVE",
"new": "1",
"answers": {
"1": {
"text": "Name",
"type":"control_fullname",
"answer": {
"first": "LeBron",
"last": "James"
},
"prettyFormat": "LeBron James"
},
"2": {
"text": "Your Message",
"type": "control_textarea",
"answer":"¡Ay, caramba!"
}
}}]
}';
foreach (json_decode($json, true)['content'] as $set) {
echo "{$set['answers'][1]['answer']['first']} {$set['answers'][1]['answer']['last']}\n";
}
Output:
LeBron James
I'm a little fuzzy on the action of the jot functions, but maybe it's supposed to be like this:
foreach ($submissions as $data) {
$detail = $jotformAPI->getSubmission($data['id']);
echo $detail['answers'][1]['answer']['first'] . '<br>';
}

Php xml data to json encode in specific format

This is my php file code where i am fetching data from thecatapi
i needed the specific format for datatable
$xml=file_get_contents('https://thecatapi.com/api/images/get?format=xml&results_per_page=3');
$xml = new SimpleXMLElement($xml);
$datas=array();
$datas['rows'] = $xml->data->images;
echo json_encode($datas, true);
Output is:
{"total":50,
"rows":{"image":[
{"url":"https:\/\/thecatapi.com\/api\/images\/get.php?id=e0i",
"id":"e0i",
"source_url":"http:\/\/thecatapi.com\/?id=e0i"
},
{"url":"https:\/\/thecatapi.com\/api\/images\/get.php?id=MTYwNDE0MQ",
"id":"MTYwNDE0MQ",
"source_url":"http:\/\/thecatapi.com\/?id=MTYwNDE0MQ"
},{
"url":"https:\/\/thecatapi.com\/api\/images\/get.php?id=bon",
"id":"bon",
"source_url":"http:\/\/thecatapi.com\/?id=bon"
}
]
}
}
but i wanted in this json form
{
"total": 800,
"rows": [
{
"url": 0,
"id": "Item 0",
"source_url": "$0"
},
{
"url": 0,
"id": "Item 0",
"source_url": "$0"
},
{
"url": 0,
"id": "Item 0",
"source_url": "$0"
},
Just came up with an alternativ solution. Change this line:
$datas['rows'] = $xml->data->images;
to this:
$datas['rows'] = $xml->data->images->image;
otherwise you can still do this
Decode the json string into an object and modify it suit your needs.
$stdClassObject = json_decode($jsonData);
$stdClassObject->rows = $stdClassObject->rows->image;
$newJsonData = json_encode($stdClassObject, JSON_PRETTY_PRINT);
echo '<pre>';
print_r($newJsonData);
echo '</pre>';
New output:
{
"total": 50,
"rows": [
{
"url": "https:\/\/thecatapi.com\/api\/images\/get.php?id=e0i",
"id": "e0i",
"source_url": "http:\/\/thecatapi.com\/?id=e0i"
},
{
"url": "https:\/\/thecatapi.com\/api\/images\/get.php?id=MTYwNDE0MQ",
"id": "MTYwNDE0MQ",
"source_url": "http:\/\/thecatapi.com\/?id=MTYwNDE0MQ"
},
{
"url": "https:\/\/thecatapi.com\/api\/images\/get.php?id=bon",
"id": "bon",
"source_url": "http:\/\/thecatapi.com\/?id=bon"
}
]
}

Laravel - Format JSON

I have a code that outputs the json result below:
{
"Ghost in the Shell": {
"id": 1203,
"Pipeline": {
"id": 6144,
"name": "Pipeline",
"status": "New",
"item_id": 5962,
"TotalTasksOpen": 2
}
}
}
Now how can I format it in the my desired json format below:
Note* I have to remove some result.
{
"Ghost in the Shell":
"id": 6144,
"name": "Pipeline",
"status": "New",
"item_id": 5962,
"TotalTasksOpen": 2
}
Will appreciate if anyone can help me please.
I am not sure what is your purpose , but here is what you need
<?php
$json = '{
"Ghost in the Shell": {
"id": 1203,
"Pipeline": {
"id": 6144,
"name": "Pipeline",
"status": "New",
"item_id": 5962,
"TotalTasksOpen": 2
}
}
}';
$array = json_decode($json, true);
$newArray = array();
foreach($array as $key=>$value) {
$newArray[$key] = '';
foreach($value as $k=>$v) {
if(is_array($v)) {
$newArray = array_merge($newArray,$v);
}
}
}
$newJson = json_encode($newArray);
echo $newJson;
?>

Separate json labels and values with php

I have accessed this json file on my website:
[{
"01:03:2016": "410",
"02:03:2016": "200",
"03:03:2016": "380"
}]
I want to use PHP to change the formatting to something like this, where the dates and counts are all values:
[{
"date": "01:03:2016",
"count": "410"
},
{
"date": "02:03:2016",
"count": "200"
},
{
"date": "03:03:2016",
"count": "380"
}]
PHP
$string = file_get_contents("data.json"); // your current json data
$json_a = json_decode($string, true);
$new_json = array();
foreach ($json_a as $key => $value) {
$new_json[] = array("date"=>$key,'count'=>$value);
}
echo json_encode($new_json); //output
data.json file (your current data)
[{
"01:03:2016": "410",
"02:03:2016": "200",
"03:03:2016": "380"
}]

How to decode JSON data and show list using PHP

I have some JSON data as follows:
{
"TABLE": [{
"ROW": [{
"COL": [{
"DATA": "Buff momo"
}, {
"DATA": "60.00"
}, {
"DATA": "1;#"
}, {
"DATA": "0"
}, {
"DATA": "1"
}, {
"DATA": "1"
}, {
"DATA": "118"
}, {
"DATA": "1;#{5D73B50D-2FFC-4D16-881E-D49328447AAB}"
}, {
"DATA": "1;#0"
}, {
"DATA": "2010-06-21 15:43:53"
}, {
"DATA": "Miscellaneous"
}, {
"DATA": "1;#Home/Lists/Canteen Menu/1_.000"
}]
}, {
"COL": [{
"DATA": "Chicken drumstick"
}, {
"DATA": "100.00"
}, {
"DATA": "3;#"
}, {
"DATA": "0"
}, {
"DATA": "1"
}, {
"DATA": "3"
}, {
"DATA": "40"
}, {
"DATA": "3;#{A1E0F087-57D3-4039-991B-E08B1CB3892A}"
}, {
"DATA": "3;#0"
}, {
"DATA": "2010-06-21 15:44:14"
}, {
"DATA": "Chinese"
}, {
"DATA": "3;#Home/Lists/Canteen Menu/3_.000"
}]
}
}]
}]
}]
}
""
I tried to decode it as follows:
header('Content-Type: application/json');
$json_string = utf8_encode(file_get_contents("filename"));
$parsed_json = json_decode($json_string, true);
foreach ($parsed_json as $key => $value) {
}
but to no avail. I am gettig errors as Invalid argument supplied for foreach().
Where am I doing wrong?
Your JSON is invalid. There are some brackets which do not belong there. The right JSON is
{ "TABLE":[{ "ROW":[ { "COL":[ {"DATA":"Buff momo"},{"DATA":"60.00"},{"DATA":"1;#"},{"DATA":"0"},{"DATA":"1"},{"DATA":"1"},{"DATA":"118"},{"DATA":"1;#{5D73B50D-2FFC-4D16-881E-D49328447AAB}"},{"DATA":"1;#0"},{"DATA":"2010-06-21 15:43:53"},{"DATA":"Miscellaneous"},{"DATA":"1;#Home/Lists/Canteen Menu/1_.000"}]}, { "COL":[ {"DATA":"Chicken drumstick"},{"DATA":"100.00"},{"DATA":"3;#"},{"DATA":"0"},{"DATA":"1"},{"DATA":"3"},{"DATA":"40"},{"DATA":"3;#{A1E0F087-57D3-4039-991B-E08B1CB3892A}"},{"DATA":"3;#0"},{"DATA":"2010-06-21 15:44:14"},{"DATA":"Chinese"},{"DATA":"3;#Home/Lists/Canteen Menu/3_.000"}]}]} ]}
you can test your JSON at http://json.parser.online.fr/
Use json_decode function for decoding json
*Updated : Example to show each COL as a list
<?php
$json_string = '{ "TABLE":[{ "ROW":[ { "COL":[ {"DATA":"Buff momo"},{"DATA":"60.00"},{"DATA":"1;#"},{"DATA":"0"},{"DATA":"1"},{"DATA":"1"},{"DATA":"118"},{"DATA":"1;#{5D73B50D-2FFC-4D16-881E-D49328447AAB}"},{"DATA":"1;#0"},{"DATA":"2010-06-21 15:43:53"},{"DATA":"Miscellaneous"},{"DATA":"1;#Home/Lists/Canteen Menu/1_.000"}]}, { "COL":[ {"DATA":"Chicken drumstick"},{"DATA":"100.00"},{"DATA":"3;#"},{"DATA":"0"},{"DATA":"1"},{"DATA":"3"},{"DATA":"40"},{"DATA":"3;#{A1E0F087-57D3-4039-991B-E08B1CB3892A}"},{"DATA":"3;#0"},{"DATA":"2010-06-21 15:44:14"},{"DATA":"Chinese"},{"DATA":"3;#Home/Lists/Canteen Menu/3_.000"}]}]} ]}';
$parsed_json = json_decode($json_string);
foreach ($parsed_json->TABLE[0]->ROW as $key => $value) {
echo 'SELECT ' . $key . ': <select name="select' . $key . '">';
foreach ($value->COL as $col) {
echo '<option value="' . $col->DATA . '">' . $col->DATA . '</option>';
}
echo '</select>';
}
?>

Categories