Javascript
JSON
Web Inspect
I have got an error message.
Uncaught ReferenceError: products is not defined.
$(document).ready(function(){
$("div").on("click", "a", function(){
var delivery_id = $(this).attr("id");
$.ajax({
url:"http://localhost:8888/dashboard/fetch_edit.php",
method:"POST",
data:{products:products},}
});
});
JSON
{
"id": "2",
"send_id": "10",
"tracking_id": "TI-000000010",
"user_id": "10",
"username": "istiaqahmed",
"email": "istiaqahmed121998#gmail.com",
"phone": "0176430886",
"company_name": "EVALY.COM.BD",
"company_phone": "01747588386",
"company_address": "Dhanmondi 28",
"com_email": "evaly#gmail.com",
"delivery_type": "Standard",
"packing": "regular",
"product_weight": "1",
"preferred_time": "morning",
"delivery_charge": "60",
"customer_name": "Zenith Jhony",
"to_address1": "75\/1 Jafrabad Pulpar Pabna House Goli",
"to_phone": "01776065208",
"to_zone": "Dhaka",
"to_post_code": "1207",
"date": "2020-01-09 23:26:10",
"u_status": "Approve",
"notes": "note",
"products": [
{
"product_id": "1",
"product_name": "shampoo",
"product_quantity": "2",
"product_price": "2333",
"customer_send_id": "10"
},
{
"product_id": "2",
"product_name": "assf",
"product_quantity": "1",
"product_price": "232",
"customer_send_id": "10"
}
]
}
You are trying to reference an attribute which does not exist.
lets say you assign that JSON object o a variable like this:
obj = {
.... the JSON
}
you could then pass the JSON as follows
data:{products:obj.products}
Hope this help you.
var products_obj = '{ "id": "2", "send_id": "10", "tracking_id": "TI-000000010", "user_id": "10", "username": "istiaqahmed", "email": "istiaqahmed121998#gmail.com", "phone": "0176430886", "company_name": "EVALY.COM.BD", "company_phone": "01747588386", "company_address": "Dhanmondi 28", "com_email": "evaly#gmail.com", "delivery_type": "Standard", "packing": "regular", "product_weight": "1", "preferred_time": "morning", "delivery_charge": "60", "customer_name": "Zenith Jhony", "to_address1": "75\/1 Jafrabad Pulpar Pabna House Goli", "to_phone": "01776065208", "to_zone": "Dhaka", "to_post_code": "1207", "date": "2020-01-09 23:26:10", "u_status": "Approve", "notes": "note", "products": [ { "product_id": "1", "product_name": "shampoo", "product_quantity": "2", "product_price": "2333", "customer_send_id": "10" }, { "product_id": "2", "product_name": "assf", "product_quantity": "1", "product_price": "232", "customer_send_id": "10" } ] }';
products_obj = JSON.parse(products_obj);
var products= products_obj.products;
console.log(products);
$(document).ready(function(){
$("div").on("click", "a", function(){
var delivery_id = $(this).attr("id");
$.ajax({
url:"http://localhost:8888/dashboard/fetch_edit.php",
method:"POST",
data:{products:products},}
});
});
Related
My MongoDB document looks something like this -
{
"VENDOR_ID": "101",
"NAME": "Test",
"DELETEFLAG": "1",
"DATETIME_MODIFIED": "2021-04-22 05:31:27am",
"CONTACT": [{
"CONTACT_ID": "111",
"CONTACT_TYPE": "CONTACT",
"DELETEFLAG": "1",
"LID": "197",
"USER_ID": "101",
"VALUE": "222222222222"
}, {
"CONTACT_ID": "222",
"CONTACT_TYPE": "EMAIL",
"DELETEFLAG": "1",
"LID": "197",
"USER_ID": "101",
"VALUE": "Test#gmail.com"
}, {
"CONTACT_ID": "333",
"CONTACT_TYPE": "CONTACT",
"DELETEFLAG": "2",
"LID": "197",
"USER_ID": "101",
"VALUE": "444444444444"
}, {
"CONTACT_ID": "444",
"CONTACT_TYPE": "CONTACT",
"DELETEFLAG": "2",
"LID": "197",
"USER_ID": "101",
"VALUE": "888888888888"
}]
}
I want to fetch all the Contacts for which the **DELETEFLAG** is set to 1. My collection can have multiple vendor documents.
I'm using PHP and fetching all the VENDORS with DELETEFLAG set to 1 along with their contacts. My query looks something like this -
$filterproduct = array(
"CONTACT.DELETEFLAG"=>'1',
"DELETEFLAG" => '1',
"LID"=>$LID,
);
$resultproduct1 = $this->GetMany($collectionName,$filterproduct,$fetchArr);
If I understand, you maybe want this:
db.collection.aggregate([
{ $unwind: "$CONTACT" },
{
$match: {
"CONTACT.DELETEFLAG": "1",
},
},
]);
Yopu break all documents in contacts with $unwind and so, get only new generated documents with CONTACT.DELETEFLAG = 1 with $match.
I'm having an issue where my android application is getting a positive result from our key:answer POST, but not from key:array for the same structure. My web developer is saying that I'm not serialising it properly, but every JSON tester I can find on the web is quite happy with the send, just not our web server. I'm sending my JSON in groups of 10 as a string (We had a previous issue where when sending about 200 keys it was giving issues, so I've split it down to 10 per send) and the JSON sent is as follows:
[{
"jobDetails": {
"jobid": "7",
"company": {
"cmid": "30"
}
},
"formName": "IPE Report",
"formHeader": "Activity",
"key": "date_inspDate",
"answervalues": "18-07-2017"
}, {
"jobDetails": {
"jobid": "7",
"company": {
"cmid": "30"
}
},
"formName": "IPE Report",
"formHeader": "Activity",
"key": "text_clientRep",
"answervalues": "testrep"
}, {
"jobDetails": {
"jobid": "7",
"company": {
"cmid": "30"
}
},
"formName": "IPE Report",
"formHeader": "Activity",
"key": "text_designCode",
"answervalues": "destdesign"
}, {
"jobDetails": {
"jobid": "7",
"company": {
"cmid": "30"
}
},
"formName": "IPE Report",
"formHeader": "Activity",
"key": "text_qcPlan",
"answervalues": "testqc"
}, {
"jobDetails": {
"jobid": "7",
"company": {
"cmid": "30"
}
},
"formName": "IPE Report",
"formHeader": "Activity",
"key": "text_equipDesc",
"answervalues": "testdesc"
}, {
"jobDetails": {
"jobid": "7",
"company": {
"cmid": "30"
}
},
"formName": "IPE Report",
"formHeader": "Activity",
"key": "text_companyName",
"answervalues": "testco"
}, {
"jobDetails": {
"jobid": "7",
"company": {
"cmid": "30"
}
},
"formName": "IPE Report",
"formHeader": "Activity",
"key": "text_drawingNo",
"answervalues": "testrev"
}, {
"jobDetails": {
"jobid": "7",
"company": {
"cmid": "30"
}
},
"formName": "IPE Report",
"formHeader": "Activity",
"key": "Detail",
"answervalues": [{
"register": 1,
"values": "act2"
}, {
"register": 2,
"values": "act3"
}]
}, {
"jobDetails": {
"jobid": "7",
"company": {
"cmid": "30"
}
},
"formName": "IPE Report",
"formHeader": "Activity",
"key": "Regulated",
"answervalues": [{
"register": 1,
"values": "YES"
}, {
"register": 2,
"values": "NO"
}]
}, {
"jobDetails": {
"jobid": "7",
"company": {
"cmid": "30"
}
},
"formName": "IPE Report",
"formHeader": "Activity",
"key": "Comments",
"answervalues": [{
"register": 1,
"values": "DIMENSIONS CHECK"
}, {
"register": 2,
"values": "HOLD POINT"
}]
}]
All is fine until it hits the Detail key, which is an array. This structure was sending fine last week, they claim they've changed nothing. Is it really a serialisation issue on my side or is the server just not accepting that key as either value or array?
From your error message:
"JSON parse error: Can not deserialize instance of java.lang.String out of START_ARRAY token;"
I think you problem is in 'answervalues'.
/* in some entries you have it as an array of objects*/
"answervalues": [{
"register": 1,
"values": "YES"
}, {
"register": 2,
"values": "NO"
}]
/* while in other entries as a string */
"answervalues": "testrev"
Talk with your developer to see how this need to be formatted. I believe the string values also need to be passed as an object in a single array. Something like..
"answervalues": [{
"register": 1,
"values":"testrev"
}]
I've been struggling around this for a while now and can't seem to get the right algorithm.
What I want is to sort this:
$variable = '{
"0": {
"id": "0",
"code": "main_colour",
"label": "Main Colour",
"options": [{
"id": "825",
"label": "White",
"products": ["1", "2", "3"]
}, {
"id": "840",
"label": "Cream",
"products": ["3", "4", "5"]
}],
"position": "0"
},
"2": {
"id": "0",
"code": "size",
"label": "Size",
"options": [{
"id": "825",
"label": "S",
"products": ["1", "2", "3"]
}, {
"id": "840",
"label": "M",
"products": ["3", "4", "5"]
}],
"position": "0"
}
}';
And the output would be
$variable = '{
"0": {
"id": "0",
"code": "main_colour",
"label": "Main Colour",
"options": [{
"id": "840",
"label": "Cream",
"products": ["3", "4", "5"]
},{
"id": "825",
"label": "White",
"products": ["1", "2", "3"]
}],
"position": "0"
},
"2": {
"id": "0",
"code": "size",
"label": "Size",
"options": [{
"id": "840",
"label": "M",
"products": ["3", "4", "5"]
}, {
"id": "825",
"label": "S",
"products": ["1", "2", "3"]
}],
"position": "0"
}
}';
So basically it should be sorted using the label inside the "options" without affecting the other fields. I've been trying usort and tried different algorithms but to no avail.
Current code that sorts only the outside "label" and not the inner label:
function optionsSort($a, $b) {
return strcmp($a['options'][0]['label'], $b['options'][0]['options']['label']);
}
usort($variable, "optionsSort"));
Here's the code:
// your array
$variable = '{
"0": {
"id": "0",
"code": "main_colour",
"label": "Main Colour",
"options": [{
"id": "825",
"label": "White",
"products": ["1", "2", "3"]
}, {
"id": "840",
"label": "Cream",
"products": ["3", "4", "5"]
}],
"position": "0"
},
"2": {
"id": "0",
"code": "size",
"label": "Size",
"options": [{
"id": "825",
"label": "S",
"products": ["1", "2", "3"]
}, {
"id": "840",
"label": "M",
"products": ["3", "4", "5"]
}],
"position": "0"
}
}';
$a = json_decode($variable);
foreach ($a as $item) {
// sorting function
usort($item->options, function($a, $b) { return strcmp($a->label, $b->label); });
}
this is driving me nuts,
I'm trying to loop throw a specific json:
I tried using
json_encode -> throws Warning: curl_close() expects parameter 1 to be resource, null given
json_decode -> shows the same text
$json->{"SKU"} and $json->SKU shows the same text
I tried to validate the json with different websites and all say the json is correct
I want to take only this values SKU, price, specialprice (with everything inside) and quantity
[{
"SKU": "118075",
"BrandName": "DIVIDED",
"CategoryName": "Correas",
"Categories": [{
"Id": "423",
"CategoryName": "Moda",
"LevelDepth": "2",
"Parent": null
}, {
"Id": "960",
"CategoryName": "Accesorios de Mujer",
"LevelDepth": "3",
"Parent": "Moda"
}],
"Name": "Divided correa Carrie",
"Description": "a",
"DescriptionShort": "v",
"MetaDescription": "Correa Divided Carrie en presentaci\u00f3n de tres colores.",
"MetaTitle": "Divided Correa Carrie",
"IsPublished": "1",
"PackageWeight": "1.000000",
"Price": 39.9,
"FinalPrice": 16.3,
"SpecialPrice": {
"StartDate": "2014-11-21 06:00:00",
"EndDate": "2017-12-31 23:59:00",
"Reduction": "20.000000",
"ReductionType": "amount"
},
"Quantity": "39",
"DateAdd": "2015-09-24 11:26:08",
"DateUpd": "2016-10-12 12:51:13",
"Images": [{
"Id": "77",
"Cover": "1",
"Legend": "Divided correa Carrie",
"url": "https:\/\/www.google.com.pe\/img\/p\/7\/7\/77.jpg"
}, {
"Id": "78",
"Cover": "0",
"Legend": "Divided correa Carrie",
"url": "https:\/\/www.google.com.pe\/img\/p\/7\/8\/78.jpg"
}, {
"Id": "79",
"Cover": "0",
"Legend": "Divided correa Carrie",
"url": "https:\/\/www.google.com.pe\/img\/p\/7\/9\/79.jpg"
}],
"FlatVariations": [{
"SKU": "001503280",
"Price": 39.9,
"FinalPrice": 16.3,
"Default": "1",
"IdImage": "77",
"Attributes": [{
"Name": "Color",
"Value": "Naranja"
}]
}, {
"SKU": "001503281",
"Price": 39.9,
"FinalPrice": 39.9,
"Default": "0",
"IdImage": "78",
"Attributes": [{
"Name": "Color",
"Value": "Blanco"
}]
}, {
"SKU": "001503282",
"Price": 39.9,
"FinalPrice": 39.9,
"Default": "0",
"IdImage": "79",
"Attributes": [{
"Name": "Color",
"Value": "Negro"
}]
}]
}]
Thanks a lot for any hint (I already tried almost every answer on stack)
I'm pretty sure you can do it in this way:
<?php
//Enter your code here, enjoy!
$text = <<<EOT
[{
"SKU": "118075",
"BrandName": "DIVIDED",
"CategoryName": "Correas",
"Categories": [{
"Id": "423",
"CategoryName": "Moda",
"LevelDepth": "2",
"Parent": null
}, {
"Id": "960",
"CategoryName": "Accesorios de Mujer",
"LevelDepth": "3",
"Parent": "Moda"
}],
"Name": "Divided correa Carrie",
"Description": "a",
"DescriptionShort": "v",
"MetaDescription": "Correa Divided Carrie en presentaci\u00f3n de tres colores.",
"MetaTitle": "Divided Correa Carrie",
"IsPublished": "1",
"PackageWeight": "1.000000",
"Price": 39.9,
"FinalPrice": 16.3,
"SpecialPrice": {
"StartDate": "2014-11-21 06:00:00",
"EndDate": "2017-12-31 23:59:00",
"Reduction": "20.000000",
"ReductionType": "amount"
},
"Quantity": "39",
"DateAdd": "2015-09-24 11:26:08",
"DateUpd": "2016-10-12 12:51:13",
"Images": [{
"Id": "77",
"Cover": "1",
"Legend": "Divided correa Carrie",
"url": "https:\/\/www.google.com.pe\/img\/p\/7\/7\/77.jpg"
}, {
"Id": "78",
"Cover": "0",
"Legend": "Divided correa Carrie",
"url": "https:\/\/www.google.com.pe\/img\/p\/7\/8\/78.jpg"
}, {
"Id": "79",
"Cover": "0",
"Legend": "Divided correa Carrie",
"url": "https:\/\/www.google.com.pe\/img\/p\/7\/9\/79.jpg"
}],
"FlatVariations": [{
"SKU": "001503280",
"Price": 39.9,
"FinalPrice": 16.3,
"Default": "1",
"IdImage": "77",
"Attributes": [{
"Name": "Color",
"Value": "Naranja"
}]
}, {
"SKU": "001503281",
"Price": 39.9,
"FinalPrice": 39.9,
"Default": "0",
"IdImage": "78",
"Attributes": [{
"Name": "Color",
"Value": "Blanco"
}]
}, {
"SKU": "001503282",
"Price": 39.9,
"FinalPrice": 39.9,
"Default": "0",
"IdImage": "79",
"Attributes": [{
"Name": "Color",
"Value": "Negro"
}]
}]
}]
EOT;
$decoded = json_decode($text);
# Index 0 because is an array
var_dump($decoded[0]->SKU);
You can execute the code directly here.
Hope this helps.
This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 9 years ago.
In CodeIgniter, in PHP to create a nested-like elements for dropdown <select> I'am using this function:
function list_categories(&$result, $cats, $sub = ''){ // <- THIS
foreach($cats as $cat){
//$cat['category']->id = $sub.$cat['category']->title;
$result[$cat['category']->id] = $sub.$cat['category']->title; // <- THIS
if( sizeof($cat['children']) > 0 ){
$sub2 = str_replace('—→ ', '–', $sub);
$sub2.= '–→ ';
list_categories($result, $cat['children'], $sub2); // <- THIS
}
}
}
// Make an array available outside the function
$categoryData = array(0 => '-- '.lang('FORM_SELECT_CATTOP').' --');
// Launch the function to fill out the array with nested sets
list_categories($categoryData, $categories);
Which generates the HTML <select> list:
I want to do the same from retrieved JSON in jQuery but can't find the answer to go deeper than first level of JSON objects with this jQuery function:
$.getJSON('../raw_get_categories/'+com_id_selected+'/0', function(data){
var items = [];
$.each(data, function(key, value, sub){
items.push('<option id="'+ key[0] + '">' + value[0] + '</option>');
});
console.log(items);
});
And this is the JSON I want to manage and retrieve the same values from it like in PHP code:
{
"28": {
"category": {
"id": "28", // <<< I need to retrieve THIS
"pid": "0",
"com_id": "2",
"route_id": "59",
"excerpt": "",
"ordering": "1",
"title": "Oblecenie a vybava",
"slug": "oblecenie-a-vybava1",
"description": "",
"image": "clothes.png",
"seo_title": "",
"meta": ""
},
"compname": {
"id": "2",
"name": "E-commerce"
},
"children": {
"61": {
"category": {
"id": "61", // <<< I need to retrieve THIS
"pid": "28",
"com_id": "2",
"route_id": "52",
"excerpt": "",
"ordering": "1",
"title": "Ubor na hlavu",
"slug": "ubor-na-hlavu1",
"description": "<p>Capice, Klobuky, Panamy, Baretky, Prilby, Kukly<br></p>",
"image": "clothes-head.png",
"seo_title": "",
"meta": ""
},
"compname": {
"id": "2",
"name": "E-commerce"
},
"children": [ ]
},
"30": {
"category": {
"id": "30",
"pid": "28",
"com_id": "2",
"route_id": "53",
"excerpt": "",
"ordering": "2",
"title": "Bundy a vetrovky",
"slug": "bundy-a-vetrovky",
"description": "",
"image": "",
"seo_title": "",
"meta": ""
},
"compname": {
"id": "2",
"name": "E-commerce"
},
"children": {
"34": {
"category": {
"id": "34",
"pid": "30",
"com_id": "2",
"route_id": "0",
"excerpt": "",
"ordering": "0",
"title": "Letne",
"slug": "letne",
"description": "",
"image": "",
"seo_title": "",
"meta": ""
},
"compname": {
"id": "2",
"name": "E-commerce"
},
"children": [ ]
},
"35": {
"category": {
"id": "35",
"pid": "30",
"com_id": "2",
"route_id": "0",
"excerpt": "",
"ordering": "0",
"title": "Vsesezonne",
"slug": "vsesezonne",
"description": "",
"image": "",
"seo_title": "",
"meta": ""
},
"compname": {
"id": "2",
"name": "E-commerce"
},
"children": [ ]
},
"33": {
"category": {
"id": "33",
"pid": "30",
"com_id": "2",
"route_id": "0",
"excerpt": "",
"ordering": "0",
"title": "Zimne",
"slug": "zimne",
"description": "",
"image": "",
"seo_title": "",
"meta": ""
},
"compname": {
"id": "2",
"name": "E-commerce"
},
"children": [ ]
}
}
},
"31": {
"category": {
"id": "31",
"pid": "28",
"com_id": "2",
"route_id": "54",
"excerpt": "",
"ordering": "3",
"title": "Nohavice a teplaky",
"slug": "nohavice-a-teplaky",
"description": "",
"image": "",
"seo_title": "",
"meta": ""
},
"compname": {
"id": "2",
"name": "E-commerce"
},
"children": [ ]
},
"32": {
"category": {
"id": "32",
"pid": "28",
"com_id": "2",
"route_id": "55",
"excerpt": "",
"ordering": "4",
"title": "Obuv",
"slug": "obuv",
"description": "",
"image": "",
"seo_title": "",
"meta": ""
},
"compname": {
"id": "2",
"name": "E-commerce"
},
"children": [ ]
}
}
},
"38": {
...
}
Could someone suggest me the appropriate way to achieve the same foreach statement in jQuery pls?
Juste use point to get more childs
$.getJSON('filename.php', function(data) {
$('#List li').remove();
$.each(data, function(index, item) {
$('#list').append('<li>'+item.category.id+'</li>');
});
});
You can do something like this:
$.each(data.actions, function(entryIndex, entry) {
var html = '<li class="top-level">' + this.action + '</li>';
});
Source