How to rewrite / restructure JSON from external URL?

Asked

Viewed 72 times

0

That is a question that I have long considered asking here at Stackoverflow. I have looked all over the internet for a solution, but I did not succeed. I did not find any subject here on Sopt.

The doubt is as follows: it is possible to rewrite, say (restructure) an existing external JSON file?

Imagine that you want to delete some objects that you don’t use, or translate objects from some API, or just make it cleaner.

Example JSON file:

{
  "content" : [ {
    "userId" : 3370,
    "year" : 2015,
    "unity" : {
      "organ" : {
        "entity" : {
          "entityId" : 2102309,
          "name" : "Jack Sparrow"
        },
    "type" : null,
    "lic" : [ ]
  }],
  "numberOfElements" : 1,
  "totalPages" : 0,
  "totalElements" : 1,
  "firstPage" : true,
  "lastPage" : false,
  "size" : 0,
  "number" : 0
}

Example - Exit - JSON File - Restructured: (outworking)

[
  {
    "usuarioId": 3370,
    "ano": 2015,
    "entidadeId": "2102309",
    "nome": "Jack Sparrow"
  }
]

What is the most efficient way to do this restructuring PHP?

I was trying in PHP something like this:

<?php
$json_strdoc = file_get_contents("https://site.com/arquivojson"); //Puxa o arquivo

    $objdoc = json_decode($json_strdoc); //Decodificando JSON

   echo "["; //Inicio Colchete

        foreach ($objdoc as $itemdoc){  //Imprimindo elementos individualmente     
            echo "                
  {
    "usuarioId": $itemdoc->content->userId,
    "ano": $itemdoc->content->year,
    "entidadeId": $itemdoc->content->unity->organ->entity->entityId,
    "nome": $itemdoc->content->unity->organ->entity->name
  },
"
   echo "]"; //Fim Colchete

?>

I am not an expert in PHP, so I am exemplifying with a code that I know is wrong, but which is more understood. I couldn’t find the right way to print the new JSON file.

  • 1

    But the idea is to be just this particular example ? What are the rules of transformation ? They are always the same ?

  • Yes, exactly @Isac =)

  • You will need to array and output with json_encode... The result will always be an array of a position?

1 answer

1


For you to bring the writing this way:

[
  {
    "usuarioId": 3370,
    "ano": 2015,
    "entidadeId": "2102309",
    "nome": "Jack Sparrow"
  }
]

You will need to store in array and return with json_encode. For example:

 $objdoc = json_decode($json_strdoc); //Decodificando JSON
 $retorno = array();
 foreach ($objdoc as $itemdoc){  //Imprimindo elementos individualmente     
     $retorno[] = array(
          "usuarioId"  => $itemdoc->content->userId,
          "ano"        => $itemdoc->content->year,
          "entidadeId" => $itemdoc->content->unity->organ->entity->entityId,
          "nome"       => $itemdoc->content->unity->organ->entity->name
        ); 
  }

 echo json_encode( $retorno );     

I hope it helps.

  • give a glance at the question I asked: https://answall.com/questions/433782/comor-filterr-e-order-elements-json-rewrites-comphp

Browser other questions tagged

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