JSON parse builder from the panel

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

So I found this code. But haven’t been able to figure out how to pass in the specific $key to filter down too. For instance, I can’t just put id, because that might exist in multiple place.

Edition: This code will spit out everything into a table. But I need to dwindle down to only a few fields. Or specific fields within the JSON data. Thus I want to be able to controll what I want from the Panel. You can see the screenshot below. I’ve also updated the screenshot above as well.



function jsonToTable ($data)
{
    $table = '
    <table class="json-table" width="100%">
    ';
    foreach ($data as $key => $value) {
        echo $key . "<br>";
      //  echo $value . "<br>";
        $table .= '
        <tr valign="top">
        ';
        if ( ! is_numeric($key)) {
            $table .= '
            <td>
                <strong>'. $key .':</strong>
            </td>
            <td>
            ';
        } else {
            $table .= '
            <td colspan="2">
            ';
        }
        if (is_object($value) || is_array($value)) {
            $table .= jsonToTable($value);
        } else {
            $table .= $value;
        }
        $table .= '
            </td>
        </tr>
        ';
    }
    $table .= '
    </table>
    ';
    return $table;
}

But I want to be able to push my tag locations into this and return out the values. The only issue is sometimes the object $key is a numberic key and has to loop through the array at this point.

May I suggest that if you want to get help from the community with this, I think you should really be much clearer about what you want to achieve.

For example, you show a panel screenshot, but the data has nothing to do with the json you show just underneath, completely different data sets.

You show some code excerpts with variables where it is not clear where they come from.

This makes it really hard to understand where you are heading.

That was an accident. I do have the right dataset. Thanks I’ll change the image now.

This here is the right image, I changed it up top too.

This image returns all the values. But in some cases maybe I don’t want to return colors,code,hex.

The function i posted above returns everything. That is nice in some cases where we might want to return everything, but in this case were we want to eliminate some data from the json, it wouldn’t work. We would need to be able to set these values in the Panel. Like in the image.