Getting Invalid JSON response in the datatable - json

I am getting the data from api and I want it to display in the datatable but I am getting the Invalid JSON response.
here is what I am doing .
My response from API
{
"status": 200,
"message": "Success",
"data": [
{
"order_id": "11204",
"ins_url": "https://www.instagram.com/p/BNXavo0hdBH/",
"quantity_total": "50",
"price": "0.0600",
"quantity_done": "0",
"status": "0",
"added_time": "1481779270"
},
{
"order_id": "11205",
"ins_url": "https://www.instagram.com/p/BNXavo0hdBH/",
"quantity_total": "50",
"price": "0.0600",
"quantity_done": "0",
"status": "0",
"added_time": "1481779413"
}]
}
My data-table code:
jQuery.post(
ajaxlink.ajax_url,
{
'action': 'getorderhistoryapi'
},
function (response) {
$('#ViewUserDatatable').DataTable({
processing: true,
serverSide: true,
columns: [
{data: "order_id"},
{ data: "ins_url" },
{ data: "quantity_total" },
{ data: "price" },
{ data: "quantity_done" },
{ data: "status" },
{ data: "added_time" }
]
});
});

Related

Error while Adding JSON data to the list in flutter

Below is the JSON data retrieved successfully from the server in flutter application
{
"error": "false",
"notification": [
{
"rn": "1",
"id": "224",
"company_details": {
"code": "2",
}
},
{
"rn": "2",
"id": "219",
"company_details": {
"code": "3",
}
},
{
"rn": "3",
"id": "213",
"company_details": {
"code": "3",
}
},
{
"rn": "4",
"id": "209",
"company_details": {
"code": "4",
}
},
{
"rn": "5",
"id": "204",
"company_details": {
"code": "3",
}
},
{
"rn": "6",
"id": "199",
"company_details": {
"code": "3",
}
},
{
"rn": "7",
"id": "193",
"company_details": {
"code": "3",
}
}
],
}
The code used here is below
List notificationsList;
getnotifications(int page) async {
Map data = {
'user_id': “VICKY,
"page":page.toString()
};
var response = await http.post(companyorders, body: data);
if(response.statusCode == 200) {
jsonResponse = json.decode(response.body);
print('Response status: ${response.statusCode}');
print('Response body: ${response.body}');
String errorcheck = jsonResponse['error'];
companyDetail = NotificationModel.fromJson(json.decode(response.body));
companyDetail = NotificationModel.fromJson(json.decode(response.body));
print('names of companies');
print(companyDetail.content);
List list = jsonResponse['notification'];
notificationsList.add(list);
}
}
When I tried to add the list to the notificationsList I got the below error
Unhandled Exception: NoSuchMethodError: The method 'add' was called on
null. Receiver: null Tried calling: add(Instance(length:7) of
'_GrowableList')
#0 Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5)
I want to add the data retrieved to the notificationsList, how should I do that
You need to initialize notificationsList:
List notificationsList = [];
Also since list is an iterable therefore use addAll()

How to filter with console.log JSON?

I need to filter in the console.log the data that I get from the json that I show in the image. How could I do it?
Code
This is my .JSON
[
{
"category": "FORMULARIOS",
"items": [
{
"label": "Botones",
"url": "ui-botones"
},
{
"label": "Inputs",
"url": "ui-inputs"
}
]
},
{
"category": "CORREOS",
"items": [
{
"label": "Facturas",
"url": "ui-facturas"
},
{
"label": "Movimientos",
"url": "ui-movimientos"
}
]
},
{
"category": "ALERTAS",
"items": [
{
"label": "Toast",
"url": "ui-toast"
},
{
"label": "Toolips",
"url": "ui-toolips"
}
]
}
]
You can do like this:
var yourJSON ="......";
var newData = filterData('CORREOS');
function filterData(catalogyName) {
return yourJSON.filter(object => {
return object['category'] == catalogyName;
});
}
console.log(newData);

How to exclude specific fields from JSON using groovy

I would like to exclude the items which don't have productModel property in the below JSON. How can we achieve this in groovy
I tried using hasProperty but not worked for me as expected. If possible can I get some sample snippet
I tried below code - but didn't work as I expected.
response.getAt('myData').getAt('data').getAt('product').hasProperty('productModel').each { println "result ${it}" }
Any help would be really appreciated.
{
"myData": [{
"data": {
"product": {
"productId": "apple",
"productName": "iPhone",
"productModel": "6s"
},
"statusCode": "active",
"date": "2018-08-07T00:00:00.000Z"
},
"links": [{
"productUrl": "test"
},
{
"productImage": "test"
}
],
"info": {}
},
{
"data": {
"product": {
"productId": "apple",
"productName": "iPhone",
"productModel": "7"
},
"statusCode": "active",
"date": "2018-08-07T00:00:00.000Z"
},
"links": [{
"productUrl": "test"
},
{
"productImage": "test"
}
],
"info": {}
},
{
"data": {
"product": {
"productId": "apple",
"productName": "Macbook"
},
"statusCode": "active",
"date": "2018-08-07T00:00:00.000Z"
},
"links": [{
"productUrl": "test"
},
{
"productImage": "test"
}
],
"info": {}
}
],
"metadata": {
"count": 3,
"offset": 0
}
}
If you want to exclude specific fields from JSON object then you have to recreate it using filtered data. The crucial part takes these two lines (assuming that json variable in the below example stores your JSON as text):
def root = new JsonSlurper().parseText(json)
def myData = root.myData.findAll { it.data.product.containsKey('productModel') }
What happens here is we access root.myData list and we filter it using findAll(predicate) method and predicate in this case says that only objects that have key productModel in path data.product are accepted. This findAll() method does not mutate existing list and that is why we store the result in variable myData - after running this method we will end up with a list of size 2.
In next step you have to recreate the object you want to represent as a JSON:
def newJsonObject = [
myData: myData,
metadata: [
count: myData.size(),
offset: 0
]
]
println JsonOutput.prettyPrint(JsonOutput.toJson(newJsonObject))
In this part we create newJsonObject and in the end we convert it to a JSON representation.
Here is the full example:
import groovy.json.JsonOutput
import groovy.json.JsonSlurper
def json = '''{
"myData": [{
"data": {
"product": {
"productId": "apple",
"productName": "iPhone",
"productModel": "6s"
},
"statusCode": "active",
"date": "2018-08-07T00:00:00.000Z"
},
"links": [{
"productUrl": "test"
},
{
"productImage": "test"
}
],
"info": {}
},
{
"data": {
"product": {
"productId": "apple",
"productName": "iPhone",
"productModel": "7"
},
"statusCode": "active",
"date": "2018-08-07T00:00:00.000Z"
},
"links": [{
"productUrl": "test"
},
{
"productImage": "test"
}
],
"info": {}
},
{
"data": {
"product": {
"productId": "apple",
"productName": "Macbook"
},
"statusCode": "active",
"date": "2018-08-07T00:00:00.000Z"
},
"links": [{
"productUrl": "test"
},
{
"productImage": "test"
}
],
"info": {}
}
],
"metadata": {
"count": 3,
"offset": 0
}
}'''
def root = new JsonSlurper().parseText(json)
def myData = root.myData.findAll { it.data.product.containsKey('productModel') }
def newJsonObject = [
myData: myData,
metadata: [
count: myData.size(),
offset: 0
]
]
println JsonOutput.prettyPrint(JsonOutput.toJson(newJsonObject))
And here is the output it produces:
{
"myData": [
{
"data": {
"product": {
"productId": "apple",
"productName": "iPhone",
"productModel": "6s"
},
"statusCode": "active",
"date": "2018-08-07T00:00:00.000Z"
},
"links": [
{
"productUrl": "test"
},
{
"productImage": "test"
}
],
"info": {
}
},
{
"data": {
"product": {
"productId": "apple",
"productName": "iPhone",
"productModel": "7"
},
"statusCode": "active",
"date": "2018-08-07T00:00:00.000Z"
},
"links": [
{
"productUrl": "test"
},
{
"productImage": "test"
}
],
"info": {
}
}
],
"metadata": {
"count": 2,
"offset": 0
}
}

Ajax to retrieve jsonp doesn't work when I call an External URL

I am having an issue getting the data from the json object.
Fist I am getting an error that complain about the json format.
$.ajax({
url: "http://www.test.com&callback=?&format=json",
type: 'POST',
dataType: "json",
beforeSend: function(xhr) {
xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
}
})
.done(function(response) {
console.log(response.assets);
})
.fail(function(response) {
console.log(response.assets);
});
Here is the json object. How I can get the
1. Total
2. Document ID
3. Medatadata values
{
"total": "1",
"included": "1",
"start": "0",
"status": "200",
"results": {
"assets": [{
"uri": "/document/id/1c0cf9cb6b8e529c8b1b0a91db37742e",
"context": "51216a3c6b28719c56d0665f538f8e3e",
"id": "1c0cf9cb6b8e529c8b1b0a91db37742e",
"path": "templatedata/rockwellautomation/publication/data/blog/VBTestDCRLocalizationUA_20170510-1102.xml",
"metadata": {
"TeamSite/Metadata/industry": "",
"TeamSite/Metadata/Locale": "en_US",
"TeamSite/Metadata/name_twitter-description": "Test DCR Localization UAT",
"TeamSite/Metadata/property_og-image": "/resources/images/rockwellautomation/share/MPI_IoT_Study_Executive_Summary_2016--photograph_1200w1200h.jpg",
"
}
]
}
}
Your json is wrong. metadata has numerous errors.
Here a good version to make it run.
{
"total": "1",
"included": "1",
"start": "0",
"status": "200",
"results": {
"assets": [{
"uri": "/document/id/1c0cf9cb6b8e529c8b1b0a91db37742e",
"context": "51216a3c6b28719c56d0665f538f8e3e",
"id": "1c0cf9cb6b8e529c8b1b0a91db37742e",
"path": "templatedata/rockwellautomation/publication/data/blog/VBTestDCRLocalizationUA_20170510-1102.xml",
"metadata": {
"TeamSite/Metadata/industry": "",
"TeamSite/Metadata/Locale": "en_US",
"TeamSite/Metadata/name_twitter-description": "Test DCR Localization UAT",
"TeamSite/Metadata/property_og-image": "/resources/images/rockwellautomation/share/MPI_IoT_Study_Executive_Summary_2016--photograph_1200w1200h.jpg"
}
}]
}
}

Returning metadata value from JSON API Response

I have an api request that returns JSON data which is then used to populate an html table. I am able to populate the table with item.name ... I would like to add the metadata value for player.field but I have not been able to access it. Here is the JSON response:
{
"list": [
{
"id": 55,
"name": "0046GS (RMS03241708)",
"description": "S 12-7 M-S 10-9",
"uuid": "6f4b5bfd-6d17-4095-9e48-7b9313f7f8c6",
"previewPlayer": false,
"enabled": true,
"mac": "00-00-00-00-00-00",
"type": "CHROMEBOX",
"distributionServer": {
"id": 2,
"name": "Main",
"driver": "IP_P2P"
},
"playergroups": [
{
"id": 2,
"name": "GameStop",
"numberOfPlayers": 454
}
],
"playerDisplays": [
{
"id": 55,
"name": "Display 1",
"channel": {
"id": 53,
"name": "GameStop TV"
},
"screenCounter": 1
}
],
"requestLogs": false,
"downloadThreads": 1,
"unusedFilesCache": 24,
"planDeliveryMethod": "CONTENT_MANAGER_DIRECT",
"pollingInterval": 1,
"pollingUnit": "MINUTES",
"logLevel": "normal",
"metadataValue": [
{
"id": 12512,
"value": "true",
"playerMetadata": {
"id": 34,
"name": "Player.field",
"datatype": "BOOLEAN",
"valueType": "ANY",
"order": 4
}
},
{
"id": 1085,
"value": "77056",
"playerMetadata": {
"id": 31,
"name": "Player.ZipCode",
"datatype": "STRING",
"valueType": "ANY",
"order": 30
}
},
{
"id": 1071,
"value": "22:15",
"playerMetadata": {
"id": 10,
"name": "Player.ScreenOff_Wednesday",
"datatype": "STRING",
"valueType": "ANY",
"order": 12
}
}
],
"usedPairingKey": "HBVULW",
"active": "ACTIVE",
"lastModified": "2017-04-03 20:12:43",
"timezoneOffset": "0"
}
],
"offset": 0,
"count": 68
}
Here is the ajax request:
$.ajax({
type: 'GET',
url: "https://sampleserver.com:8080/ContentManager/api/rest/players?limit=1&offset=0&sort=name&filters=%7BplayerStatus%20:%20%7Bvalues:%5B'ACTIVE'%5D,%20comparator%20:%20'eq'%7D%7D",
dataType: "json",
success: function(data) {
$.each(data.list, function(i, item) {
var tr = $('<tr><td>'+item.name+'</td><td>'+some.JSONResponse+'</td></tr>').appendTo('#scalaapi');
});
}
});
I am stuck on how to specifically display the value for player.field ... "value": "true",
It feels like it should be simple, but my attempts have all been met with undefined.
Attempted if statement...
success: function(data) {
$.each(data.list, function(i, item) {
var tr = $('<tr><td>'+item.name+'</td><td class="val">...</td></tr>').appendTo('#scalaapi');
var bool = function(i,item) {if (item.metadataValue.id = '12512');
tr.find('.val').text(bool);};
});
}
});