Transform JSON result to 'uppercase'

Asked

Viewed 674 times

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 inside the "Prod" -> "text" keys in uppercase 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 of using a function in javascript, instead of php, to change the result of JSON, leaving it uppercase.

So far the above code is not working.

  • vc want the array keys in upper case or only the value?

  • only the value, @rray

  • You only need to present these values in uppercase on a web page?

  • @rocmartins no, I need to turn the JSON into uppercase. It is for the purpose of searching on the site.

  • In this case, I believe the most correct approach is to make a search that is case insensitive.

  • @rocmartins has some material to send me?

  • I got it using stristr instead of strto. Was asism: if(stristr($Collection['Prod']['text'],$string) !== false) { echo...

  • @Gustavocave depends a lot on how you are doing the search. I don’t understand what the search structure is and what you need JSON for.

  • Your JSON is still wrong! Missing the closure with ] and }

Show 4 more comments

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 again to json

$meu_json_tudo_maiusculo = json_encode($array);

UPDATING

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’s why it’s not working!

Another observation: Your JSON is poorly formed.

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

http://ideone.com/cYx6Zh

When JSON is poorly formed, it returns false when using json_decode. Maybe it’s in case you check if this is happening with your code.

Considering also that problems have been reported regarding the image link, you can simply ignore the urls in transforming the json content.

Then the code will look 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

  • I’m working on that code you sent me, so far with no results.

  • $meu_json should be the JSON string. Remembering that your json is poorly formed (if you can put it completely in the question)

  • edited the question.

  • There’s an error in your code. You didn’t put the & in the parameter!

  • I put it in and it didn’t work.

  • I tried so also @Wallace Maxters , but it didn’t work. $upper = array($Collection['Prod']['text']); Return(array_change_key_case($upper,CASE_UPPER));

  • I was able to make it work the way you said, but the images are gone. Code: array_walk_recursive($Results, Function (&$value) { $value = mb_strtoupper($value, 'UTF-8'); }); $meu_json_tudo_maiusculo = json_encode($Results);

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.