Create a JSON Object or a JSON Array?

-1

It turns out that I have three tables, tablaA , tablaB , tablaC and I want to generate a JSON with it to be able to send it to another different base, but I want to separate the actions because in the tables there are insert and updates that are differentiate by a field called accion in each table.

The question is how would the structure of the JSON that keeps all that data and then be able to traverse it and make the insert or the update depend on what comes?

I've tried:

{
  tablaA: 
     insert: {
       campo:valor,
       campo:valor
     }
     update: {
       campo:valor,
       campo:valor
     }
 tablaB: 
   insert:{
     campo:valor,
     campo:valor
   }
   update:{
     campo:valor,
     campo:valor
   }
}
    
asked by Igmer Rodriguez 08.09.2018 в 08:06
source

2 answers

1

Maybe this can be useful

PHP

<?
$tablas=array("tablaA","tablaB","tablaC");
$acciones=array("insert","update");
$campos=array(array("campoA","valorA","CondicionA"),array("campoB","valorB","CondicionB"),array("campoC","valorC","CondicionC"));

$json=array();
foreach($tablas as $row){
    foreach($acciones as $row2){
        foreach($campos as $row3){
            $json[$row][$row2][]=array($row3[0],$row3[1],$row3[2]);
        }
    }
}
$json=json_encode($json);
?>

PHP code emulates loading the fix

JS

<script>
var Arreglo=$.parseJSON('<?=$json?>');
$.each(Arreglo,function(key,value){
    $.each(value,function(key2,value2){
        $.each(value2,function(key3,value3){
            var SQL="";
            if(SQL!="") SQL+=", ";
            if(key2=="insert") SQL+=value3[0]+"="+value3[1];
            if(key2=="update") SQL+=value3[0]+"="+value3[1]+" WHERE "+value3[2];
            if(key2=="insert") SQL="INSERT INTO "+key+" SET "+SQL;
            if(key2=="update") SQL="UPDATE "+key+" SET "+SQL;
            console.log(SQL);
        });
    });
});
</script>

The JS code traverses that array and generates an SQL string for each element

The answer of the console is this

INSERT INTO tablaA SET campoA=valorA
INSERT INTO tablaA SET campoB=valorB
INSERT INTO tablaA SET campoC=valorC
UPDATE tablaA SET campoA=valorA WHERE CondicionA
UPDATE tablaA SET campoB=valorB WHERE CondicionB
UPDATE tablaA SET campoC=valorC WHERE CondicionC
INSERT INTO tablaB SET campoA=valorA
INSERT INTO tablaB SET campoB=valorB
INSERT INTO tablaB SET campoC=valorC
UPDATE tablaB SET campoA=valorA WHERE CondicionA
UPDATE tablaB SET campoB=valorB WHERE CondicionB
UPDATE tablaB SET campoC=valorC WHERE CondicionC
INSERT INTO tablaC SET campoA=valorA
INSERT INTO tablaC SET campoB=valorB
INSERT INTO tablaC SET campoC=valorC
UPDATE tablaC SET campoA=valorA WHERE CondicionA
UPDATE tablaC SET campoB=valorB WHERE CondicionB
UPDATE tablaC SET campoC=valorC WHERE CondicionC
    
answered by 08.09.2018 / 10:20
source
0

I have managed to understand how JSON works

JSONObjects are composed of a key = > value like this: {"id": 1}

The JSONArray can have many values within it like this [1,2,3,4,5]

What I did was create an insert array and an update array for each table and then all that within a JSONObject

JSONObject json = new JSONObject
JSONArray jarray = new JSONArray
jarray.put("primer insert", "segundo insert"...)
json.put("insert",jarray);

And so I have created more array and I have placed them inside the JSONObject

    
answered by 09.09.2018 в 19:55