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.
But the idea is to be just this particular example ? What are the rules of transformation ? They are always the same ?
– Isac
Yes, exactly @Isac =)
– Alexandre Lopes
You will need to array and output with json_encode... The result will always be an array of a position?
– adventistaam