Here’s what I’m trying to build. It gets a bit more complicated but I’m trying to keep it more basic now. Here’s my usecase.
User can take any webservice. And with little knowledge of parsing JSON, can put the webservice link in the text box. Then I will run CURL against that return json. Then from the tags they specified in a structured field. They will be able to parse to that field.
It would look like this in the panel. Then it would loop through and parse this.
{
"colors": [
{
"color": "black",
"category": "hue",
"type": "primary",
"code": {
"rgba": [255,255,255,1],
"hex": "#000"
}
},
{
"color": "white",
"category": "value",
"code": {
"rgba": [0,0,0,1],
"hex": "#FFF"
}
},
{
"color": "red",
"category": "hue",
"type": "primary",
"code": {
"rgba": [255,0,0,1],
"hex": "#FF0"
}
},
{
"color": "blue",
"category": "hue",
"type": "primary",
"code": {
"rgba": [0,0,255,1],
"hex": "#00F"
}
},
{
"color": "yellow",
"category": "hue",
"type": "primary",
"code": {
"rgba": [255,255,0,1],
"hex": "#FF0"
}
},
{
"color": "green",
"category": "hue",
"type": "secondary",
"code": {
"rgba": [0,255,0,1],
"hex": "#0F0"
}
},
]
}
I parse it like this normally when it doens’t hit an array. When it hits an array like this I would need to concate the values because they are going to be stored in a table.
I was doing something like this. Then counting the tags.
$tags = $event->jsontags()->split(',');
$total = count($tags);
switch ($total) {
case "1":
$a = $tags[0];
if (isset($item[$a])) {
$jsonValue = $item[$a];
echo '<td>' . $jsonValue . '</td>';
}
else{
$jsonValue = '';
echo '<td>' . $jsonValue . '</td>';
}
break;
case "2":
$a = $tags[0];
$b = $tags[1];
if (isset($item[$a][$b])) {
$jsonValue = $item[$a][$b];
echo '<td>' . $jsonValue . '</td>';
}
else{
$jsonValue = 'notin';
echo '<td>' . $jsonValue . '</td>';
}
break;
case "3":
$a = $tags[0];
$b = $tags[1];
$c = $tags[2];
$jsonValue = $item[$a][$b][$c];
echo '<td>'.$jsonValue.'</td>';
break;
default:
echo "Your favorite color is neither red, blue, nor green!";
}
But this does not solve the issue when the code hits what should be a loop.
If anybody can think of a creative way to contribute to this. This would be an awesome post because it would be nice to drop any webservice link into Kirby and have it spit out a table of information. I know that parsing with PHP can be tricky also.
This is how I’m getting and passing in my JSON
$resp = json_decode($getResponse, true);
If I need to add more of what I’m doing to try and solve this I can, but If someone knows of a simple function or way to solve this, I don’t want to impress what I’m trying because there might be an easier way.
Thanks