Convert PHP Json to jquery format - javascript

I make this function in php, but i want to make now in Jquery, is the same process to build a this json format in jquery?
I need the same format like this PHP, but now in jquery
$json_full = array();
$json= (object) array (
"salutation"=>'test' ,
"title"=>'test' ,
"first_name"=>'test' ,
"last_name"=>'test' ,
"street"=>'test' ,
"street_number"=>'test' ,
"address_supplement"=>'test' ,
"zipcode"=>'test' ,
"city"=>'test' ,
"country"=>'test' ,
"terms_accepted"=>true,
"receiving_mails_accepted"=>'test' ,
"email"=>'test' ,
"lottery_accepted"=> false,
"lottery_solution"=> "LOTTERY",
"original_created_at"=>'test' ,
);
$json->items = (object )array (
'campaign_number' =>'test' ,
'item_number' =>'test' ,
);
array_push($json_full, $json);
print_r(json_encode($json_full));

In javascript you can do this:
var json = { // Create object
"salutation" : "test",
"title" : "test",
"lottery_accepted" : true, // Boolean
"items" : [{ // Array of objects
"campaign_number" : "test",
"item_number" : "test"
}],
...
};
var jsonfull = []; // Push to jsonfull.
jsonfull.push(json);
Convert to string:
JSON.stringify(json);

Related

Symfony 4 - PHP convert multidimensional array to javascript

Hello I've this array in Symfony on my controller:
$array = [
"label" => [
"january",
"february"
],
"data" => [
0,
1
]
];
I wish I could convert it for use in Javascript.
The goal is that I can get in JS:
["january", "february"]
and
[0,1]
to use them as array variables
I tried json_encode($array), it works but I can't access to my array using {{array["label"}} in Twig in the Javascript block
Can someone help me please ?
EDIT : Okay guys, it works now, I did this :
Controller :
return $this->render('products/index.html.twig', [
"report" => json_encode($report),
]);
index.html.twig (javascript bloc)
const data = {{report | raw}};
Thanks all !
You do not have to call JSON.parse since the output of json_decode is a javascript literal. Just assign it to a variable.
var yourArray = <?php echo json_encode($array); ?>;
You can access property like this
alert(yourArray[0].Key);
You can try use:
PHP:
$array = [
"label" => [
"january",
"february"
],
"data" => [
0,
1
]
];
$response = new Response(json_encode($array));
$response->headers->set('Content-Type', 'application/json');
return $response;
JavaScript:
var data = JSON.parse(response);
console.log(data.label)
Hope help you.
What you get is a Json string, not an object, you first have to parse it in Javascript.
const data=JSON.parse(array);

How can I merge a new object to existing object in an array in javascript?

I have array like above. I want to input {"temperature":{"work":30,"home":24}} object to the first of array.
So array should start with:
0 : {title : "tptp", {"temperature":{"work":30,"home":24}}, lastview:"12-12 21:2"}
My code is
console.log("below is home");
console.log(this.home);
console.log(this.home[0].push({"temperature": {"work":30,"home":24}}));
But I have error TypeError: this.home[0].push is not a function.
The expected output you posted isn't possible, i.e.
{title : "tptp", {"temperature":{"work":30,"home":24}}, lastview:"12-12 21:2"}
what you probably want is
{title : "tptp", "temperature":{"work":30,"home":24}, lastview:"12-12 21:2"}
which you can achieve with
Object.assign(this.home[0], {"temperature": {"work":30,"home":24}})
push function works with Array, not with Object and
{title : "tptp", {"temperature":{"work":30,"home":24}}, lastview:"12-12 21:2"} // Ivalid Json
You have to interduce new key temperature according to your requirement.
console.log("below is home");
console.log(this.home);
this.home[0]["temperature"] = {"work":30,"home":24};
console.log( this.home[0] );
Output will be
[
{
"title": "tptp",
"lastview": "12-12 21:2",
"temperature": {
"work": 30,
"home": 24
}
},
{
"title": "gngn",
"lastview": "12-12 19:29"
}
]
What you can do is ,
this.home[0].put("temperature": {"work":30,"home":24});

Call json response data in javascript

i have json response like this :
{
"gasal": [
{
"prodi": "Teknik Industri",
"jumlah": "3.5000"
},
{
"prodi": "Teknik Informatika",
"jumlah": "6.0000"
}
],
"genap": [
{
"prodi": "Teknik Informatika",
"jumlah": "2.0000"
}
],
"prodi": [
{
"nama_prod": "Teknik Informatika"
},
{
"nama_prod": "Teknik Industri"
}
]}
i want to show the response to bar chart (i use chart js), but the problem is i dont know how to call the json response in javascript.
this is my javascript code :
var url = "{{url('test')}}";
var Prodi = new Array();
var gasal = new Array();
var genap = new Array();
$.get(url, function(response){
response.forEach(function(data){
Gasal.push(data.genap.jumlah);
Genap.push(data.gasal.jumlah);
Prodi.push(data.prodi.nama_prod);
});
})
i think the problem is in this code
Gasal.push(data.genap.jumlah);
Genap.push(data.gasal.jumlah);
Prodi.push(data.prodi.nama_prod);
what is the right code ?
thank you.
Gasal.push(data.genap[0]) would be the whole object
Gasal.push(data.genap[0].jumlah) would be the property of the object
Because you have created an array with an object inside and then you need to access the properties of the object.

Converting mysql exported json object to a properly formatted javascript array of objects

Have got a json file exported from mysql. One particular line is not a well represented json object, i'm trying to convert this to a proper array of object.
var data = "{"54":
{"ID":"54",
"QTY":"1",
"NAME":"Large",
"TOTAL":1.86
},
"TOTAL":10.54,
"313":
{"ID":"313",
"QTY":2,
"NAME":"Quater Pounder",
"TOTAL":8.68
}
}"
//and wants to make it:
var data = [
{"ID" : "54",
"QTY" : "1",
"NAME": "Quarter Pounder",
"TOTAL": 8.68
},
{"ID":"313",
"QTY":2,
"NAME": "Quater Pounder",
"TOTAL":8.68
}
]
I was able to fix this by using angular.forEach(response, function(item){}), I then created a childArray, which I pushed the result of the above into.
Please see code:
angular.forEach( $scope.response, function (item) {
item.childrenList = [];
angular.forEach( JSON.parse( item.details ), function (value, id) {
item.childrenList.push( value );
})
});

get MongoDB ObjectId in JSON via Javascript

This is my JSON from PHP:
{"data" :
[
{
"_id" : {
"$id" : "4f977259b1445dce24000000"
},
"headline" : "asdfasdf",
"date" : {
"sec" : 1333584000,
"usec" : 0
},
"text":"asdfasdfas"
}
]
}
In Javascript I want to use the values and it works fine with
obj = JSON.parse(request);
console.log(obj.data[i].headline);
But how do I get the ObjectId?
It does not work like this:
console.log(obj.data[i]._id.$id);
It seems to work as expected when I try it http://jsfiddle.net/2CSWr/
console.log(json.data[0]._id.$id);​
does output the right value
This works for me:
console.log(obj.data[0]._id['$id']);

Categories