Transform JSON result to uppercase

3

I have the following JSON (complete):

{
  "name": "Romano Pesca",
  "count": 8,
  "frequency": "Weekly",
  "version": 5,
  "newdata": false,
  "lastrunstatus": "success",
  "thisversionstatus": "success",
  "nextrun": "Sat Aug 08 2015 15:14:57 GMT+0000 (UTC)",
  "thisversionrun": "Sat Aug 01 2015 15:14:57 GMT+0000 (UTC)",
  "results": {
    "collection1": [
      {
        "img": {
          "alt": "Vara Sumax Victória 9'0\" ( 2,70m ) Para Carretilha",
          "href": "http://www.romanopesca.com.br/vara-sumax-victoria-9-0-2-70m-para-carretilha.php",
          "src": "http://www.romanopesca.com.br/media/catalog/product/cache/1/small_image/135x/9df78eab33525d08d6e5fb8d27136e95/s/u/sumax-080357_1.jpg",
          "text": ""
        },
        "prod": {
          "href": "http://www.romanopesca.com.br/vara-sumax-victoria-9-0-2-70m-para-carretilha.php",
          "text": "Vara Sumax Victória 9'0\" ( 2,70m ) Para Carretilha"
        },
        "valor": "R$242,91",
        "index": 1,
        "url": "http://www.romanopesca.com.br/"
      },

I need to put the values that are inside the keys "prod" - > "text" in CAPITAL LETTER.

    "results": {
    "collection1": [
      {
        "prod": {
          "text": "ARA SUMAX VICTÓRIA 9'0" ( 2,70M ) PARA CARRETILHA"
        },

I have the following code for php cases:

    <?php
$request ="https://www.kimonolabs.com/api/7fzc196k?apikey=9TCUO9EskMyL4HtmqHMNDIiaZ9KmOcXn";
$response = file_get_contents($request);
$results = json_decode($response, TRUE);
array_walk_recursive($results, function ($value)
{
   $value = mb_strtoupper($value, 'UTF-8');
});

$meu_json_tudo_maiusculo = json_encode($results);

?>

I also thought about using a function in javascript, instead of php, to change the JSON result, leaving it uppercase.

So far the above code is not working.

    
asked by anonymous 01.08.2015 / 18:50

1 answer

2

I'll leave my answer in case you do this in PHP.


$array =  json_decode($meu_json, true);

array_walk_recursive($array, function (&$value)
{
   $value = mb_strtoupper($value, 'UTF-8');
});

print_r($array);

To convert back to json

$meu_json_tudo_maiusculo = json_encode($array);

UPDATE

Since you copied my answer to put it in your question, I would like to point out that you are not using the reference operator & and maybe that is why it is not working!

Another note: Your JSON is malformed.

See in this example that, my JSON being properly formed, works.

link

When JSON is malformed, it returns false when using json_decode . Maybe it's the case for you to check if this is happening with your code.

Considering also that problems were reported regarding the image link, you can simply ignore the urls in the json content transformation.

Then the code looks like this:


$array =  json_decode($meu_json, true);

array_walk_recursive($array, function (&$value)
{
   if (filter_var($value, FILTER_VALIDATE_URL)) return;

   $value = mb_strtoupper($value, 'UTF-8');
});

print_r($array);

See this example on IDEONE

    
01.08.2015 / 19:26