How to add anther "entry" into this JSON? - javascript

I downloaded some JSON, as shown below. In the developer console it looks like an object containing nested objects which contain nested objects ...
I would like to add a new, empty "campaign" into the data (at the front). How do I do that?
Insert
var blankCampaignData = {'title': '', 'description': '', 'path_to_logo': '', 'start_time': date, 'end_time' : date, 'paused': false};
into
{
"campaigns": {
"1": {
"campaign_id": "1",
"title": "Nike Air 2015 campaign",
"description": null,
"path_to_logo": null,
"start_time": "09/11/2015 22:42:08",
"end_time": "09/03/2016 22:42:08",
"paused": "0",
"destinations": {
"1": {
"destination_id": "1",
"campaign_id": "1",
"url": "www.nike.com/nike_air",
"description": "Nike air destination",
"connections": {
"3": {
"connection_id": "3",
"destination_id": "1",
"tag_id": "0",
"country": "Scotland",
"county": "Yorkshire",
"town": "East Ham",
"post_code": "SE1 1AA",
"custom": "bus stop",
"description": "Connection number 3"
}
}
},
"2": {
"destination_id": "2",
"campaign_id": "1",
"url": "www.nike.com/nike_air/sub_campaign",
"description": "Nike air - free laces promotion destination",
"connections": {
"2": {
"connection_id": "2",
"destination_id": "2",
"tag_id": "0",
"country": "Engerland",
"county": "Devon",
"town": "East Ham",
"post_code": "SE1 1AA",
"custom": "bus stop",
"description": "Connection number 2"
},
"4": {
"connection_id": "4",
"destination_id": "2",
"tag_id": "0",
"country": "Engerland",
"county": "Yorkshire",
"town": "Felixswtowe",
"post_code": "RB3 9YR",
"custom": "police staticon",
"description": "Connection number 4"
},
"6": {
"connection_id": "6",
"destination_id": "2",
"tag_id": "0",
"country": "Scotland",
"county": "Essex",
"town": "York",
"post_code": "JD8 4LF",
"custom": "somewhere else",
"description": "Connection number 6"
},
"9": {
"connection_id": "9",
"destination_id": "2",
"tag_id": "0",
"country": "Scotland",
"county": "Cork",
"town": "York",
"post_code": "JD8 4LF",
"custom": "in the ladies' loo",
"description": "Connection number 9"
}
}
}
}
},
"2": {
"campaign_id": "2",
"title": "Nike football boots campaign",
"description": null,
"path_to_logo": null,
"start_time": "09/12/2015 22:42:08",
"end_time": "09/01/2016 22:42:08",
"paused": "0",
"destinations": {
"3": {
"destination_id": "3",
"campaign_id": "2",
"url": "www.nike.com/nike_football_boots/",
"description": "Nike footie boots destination",
"connections": {}
},
"4": {
"destination_id": "4",
"campaign_id": "2",
"url": "www.nike.com/nike_football_boots/sub_campaign",
"description": "Buy left boot, get right boot free destination",
"connections": {}
}
}
},
"3": {
"campaign_id": "3",
"title": "Nike general promotion campaign",
"description": null,
"path_to_logo": null,
"start_time": "09/12/2013 22:42:08",
"end_time": "09/08/2016 22:42:08",
"paused": "0",
"destinations": {
"5": {
"destination_id": "5",
"campaign_id": "3",
"url": "www.nike.com/general_promotion",
"description": "Nike general promotion destination",
"connections": {}
},
"6": {
"destination_id": "6",
"campaign_id": "3",
"url": "www.nike.com/general_promotion/discount_coupon",
"description": "20% off coupon destination",
"connections": {}
}
}
}
}
}

Work out what the next campaign id should be:
var nextId = +Object.keys(obj.campaigns).sort().pop() + 1;
Add the empty campaign to the campaigns object. Obviously you'll need to define date beforehand.
obj.campaigns[nextId] = {
'title': '',
'description': '',
'path_to_logo': '',
'start_time': date,
'end_time' : date,
'paused': false
}

Related

Merging two array of objects only when array element contains error

So I have two arrays with objects. And also an array indicating where the replacement should take place(It spots if object contains error)
const oldData = [
{
"index": "01",
"skuId": "Sarbb-033",
"name": "Sasko Black",
"barcode": "843331510012",
"description": "Nice black bread",
"brand": "ERROR: No brand matching: Sasko",
"productLine": "ERROR: No product line matching: line",
"inputType": "Weight",
"uom": "ERROR: Invalid UoM type, valid values are: kg,g,mg,kl,l,ml,m,cm,mm",
"value": "700",
"capacity": "1",
"image": ""
},
{
"index": "02",
"skuId": "ERROR: Empty sku_id is not allowed",
"name": "Sasko Black1",
"barcode": "ERROR: Empty barcode is not allowed",
"description": "Nice black bread",
"brand": "ERROR: No brand matching: Future Life",
"productLine": "ERROR: No product line matching: line",
"inputType": "Weight",
"uom": "kg",
"value": "701",
"capacity": "2",
"image": ""
},
{
"index": "03",
"skuId": "Sarbb-099",
"name": "Sasko Black100",
"barcode": "843332555614",
"description": "Nice black bread",
"brand": "fwfwf",
"productLine": "naam",
"inputType": "weight",
"uom": "g",
"value": "702",
"capacity": "3",
"image": ""
},
{
"index": "04",
"skuId": "Sarbb-100",
"name": "Sasko Black101",
"barcode": "ERROR: Empty barcode is not allowed",
"description": "ERROR: Invalid description: [] it should not be blank.",
"brand": "fwfwf",
"productLine": "fwfwf",
"inputType": "Weight",
"uom": "g",
"value": "703",
"capacity": "4",
"image": ""
},
{
"index": "05",
"skuId": "Sarbb-101",
"name": "Sasko Black102",
"barcode": "843332555616",
"description": "Nice black bread",
"brand": "fwfwf",
"productLine": "naam",
"inputType": "weight",
"uom": "g",
"value": "704",
"capacity": "5",
"image": ""
}
]
const newData = [
{
"index": "01",
"skuId": "Sarbb-033",
"name": "Sasko Black",
"barcode": "843331510012",
"description": "Nice black bread",
"brand": "fwfwf",
"productLine": "fwfwf",
"inputType": "Weight",
"uom": "g",
"value": "700",
"capacity": "1",
"image": ""
},
{
"index": "02",
"skuId": "sarb",
"name": "Sasko Black1",
"barcode": "124125125",
"description": "Nice black bread",
"brand": "fwfwf",
"productLine": "fwfwf",
"inputType": "Weight",
"uom": "kg",
"value": "701",
"capacity": "2",
"image": ""
},
{
"index": "03",
"skuId": "Sarbb-100",
"name": "Sasko Black101",
"barcode": "214214214",
"description": "Desc",
"brand": "fwfwf",
"productLine": "fwfwf",
"inputType": "Weight",
"uom": "g",
"value": "703",
"capacity": "4",
"image": ""
}
]
const errorRows = [0,1,3]
const myTerribleAttempt = oldTableData.map((oldData, rowIndex) => {
return errorRows.map(index => {
if(rowIndex === index){
newTableData.map(newData => {
oldData = newData
})
}
})
})
I have tried multiple maps and just can't seem to get the right result. The new data objects should replace old data objects at the object containing the error. Please give me some assistance.
I think you've overcomplicated this - just check if the index is contained in the error array and choose old or new data.
const result = oldData.map( (item, idx) => errorRows.includes(idx)
? newData[errorRows.findIndex(x => x == idx)]
: item);
live example:
const oldData = [{
"index": "01",
"skuId": "Sarbb-033",
"name": "Sasko Black",
"barcode": "843331510012",
"description": "Nice black bread",
"brand": "ERROR: No brand matching: Sasko",
"productLine": "ERROR: No product line matching: line",
"inputType": "Weight",
"uom": "ERROR: Invalid UoM type, valid values are: kg,g,mg,kl,l,ml,m,cm,mm",
"value": "700",
"capacity": "1",
"image": ""
},
{
"index": "02",
"skuId": "ERROR: Empty sku_id is not allowed",
"name": "Sasko Black1",
"barcode": "ERROR: Empty barcode is not allowed",
"description": "Nice black bread",
"brand": "ERROR: No brand matching: Future Life",
"productLine": "ERROR: No product line matching: line",
"inputType": "Weight",
"uom": "kg",
"value": "701",
"capacity": "2",
"image": ""
},
{
"index": "03",
"skuId": "Sarbb-099",
"name": "Sasko Black100",
"barcode": "843332555614",
"description": "Nice black bread",
"brand": "fwfwf",
"productLine": "naam",
"inputType": "weight",
"uom": "g",
"value": "702",
"capacity": "3",
"image": ""
},
{
"index": "04",
"skuId": "Sarbb-100",
"name": "Sasko Black101",
"barcode": "ERROR: Empty barcode is not allowed",
"description": "ERROR: Invalid description: [] it should not be blank.",
"brand": "fwfwf",
"productLine": "fwfwf",
"inputType": "Weight",
"uom": "g",
"value": "703",
"capacity": "4",
"image": ""
},
{
"index": "05",
"skuId": "Sarbb-101",
"name": "Sasko Black102",
"barcode": "843332555616",
"description": "Nice black bread",
"brand": "fwfwf",
"productLine": "naam",
"inputType": "weight",
"uom": "g",
"value": "704",
"capacity": "5",
"image": ""
}
]
const newData = [{
"index": "01",
"skuId": "Sarbb-033",
"name": "Sasko Black",
"barcode": "843331510012",
"description": "Nice black bread",
"brand": "fwfwf",
"productLine": "fwfwf",
"inputType": "Weight",
"uom": "g",
"value": "700",
"capacity": "1",
"image": ""
},
{
"index": "02",
"skuId": "sarb",
"name": "Sasko Black1",
"barcode": "124125125",
"description": "Nice black bread",
"brand": "fwfwf",
"productLine": "fwfwf",
"inputType": "Weight",
"uom": "kg",
"value": "701",
"capacity": "2",
"image": ""
},
{
"index": "03",
"skuId": "Sarbb-100",
"name": "Sasko Black101",
"barcode": "214214214",
"description": "Desc",
"brand": "fwfwf",
"productLine": "fwfwf",
"inputType": "Weight",
"uom": "g",
"value": "703",
"capacity": "4",
"image": ""
}
]
const errorRows = [0, 1, 3];
const result = oldData.map((item, idx) => errorRows.includes(idx) ? newData[idx] : item);
console.log(result);

Why is my Data sent as Undefined when using Tabulator?

I am trying to insert JSON data that are supposed to be search results into my Tabulator, which is then supposed to display the search results in their respective columns. Here is my code:
<body>
<div id="example-table"></div>
<script>
var table = new Tabulator("#example-table", {
ajaxURL:"http://hadrians-search.tk/search",
ajaxParams:{search_param:"ball", items_per_page:"2", page_number:"2"},
ajaxResponse:function(url, params, response){
//url - the URL of the request
//params - the parameters passed with the request
//response - the JSON object returned in the body of the response.
return response.Object; //return the d property of a response json object
},
columns:
[{title:"Title", field:"shippingCost.title"},
{title:"Price", field:"price"},
{title:"Shipping Cost", field:"shippingCost.shippingServiceCost.value"},
{title:"Shipping Type", field:"shippingCost.shippingServiceCost.shippingType"},
],
});
</script>
</body>
I am receiving this in the Console tab of my browser:
Data Loading Error - Unable to process data due to invalid data type
Expecting: array
Received: undefined
Data: undefined tabulator.min.js:2:29478
n.prototype._setDataActual
https://unpkg.com/tabulator-tables#4.1.2/dist/js/tabulator.min.js:2:29478
n.prototype.setData/<
https://unpkg.com/tabulator-tables#4.1.2/dist/js/tabulator.min.js:2:28800
n.prototype.setData
https://unpkg.com/tabulator-tables#4.1.2/dist/js/tabulator.min.js:2:28624
f.prototype._loadDataStandard/</<
https://unpkg.com/tabulator-tables#4.1.2/dist/js/tabulator.min.js:5:7539
This is the JSON Response I am sending to the Tabulator:
{
"0": {
"country": "US",
"itemId": "323440622675",
"price": "11.02",
"shippingCost": {
"expeditedShipping": "false",
"handlingTime": "3",
"oneDayShippingAvailable": "false",
"shipToLocations": "Worldwide",
"shippingServiceCost": {
"_currencyId": "USD",
"value": "0.0"
},
"shippingType": "Free"
},
"title": "Magnetic Pearl Ball Curtain Tiebacks Tie Backs Holdbacks Buckle Clips Accessory",
"user_args": {
"advanced": null,
"pages": {
"entries_per_page": 10,
"page_number": 10
},
"search_terms": "ball"
}
},
"1": {
"country": "CN",
"itemId": "332746804737",
"price": "2.49",
"shippingCost": {
"expeditedShipping": "false",
"handlingTime": "1",
"oneDayShippingAvailable": "false",
"shipToLocations": "Worldwide",
"shippingServiceCost": {
"_currencyId": "USD",
"value": "0.0"
},
"shippingType": "Free"
},
"title": "Natural Amethyst Quartz Stone Sphere Crystal Fluorite Ball Healing Gemstone",
"user_args": {
"advanced": null,
"pages": {
"entries_per_page": 10,
"page_number": 10
},
"search_terms": "ball"
}
},
"2": {
"country": "US",
"itemId": "322315462251",
"price": "5.49",
"shippingCost": {
"expeditedShipping": "true",
"handlingTime": "1",
"oneDayShippingAvailable": "false",
"shipToLocations": "Worldwide",
"shippingServiceCost": {
"_currencyId": "USD",
"value": "4.89"
},
"shippingType": "Flat"
},
"title": "Richardson Trucker Ball Cap Meshback Hat Snapback Cap Trucker Hat Cap - 112",
"user_args": {
"advanced": null,
"pages": {
"entries_per_page": 10,
"page_number": 10
},
"search_terms": "ball"
}
},
"3": {
"country": "US",
"itemId": "183411812494",
"price": "22.99",
"shippingCost": {
"expeditedShipping": "true",
"handlingTime": "0",
"oneDayShippingAvailable": "false",
"shipToLocations": "Worldwide",
"shippingServiceCost": {
"_currencyId": "USD",
"value": "0.0"
},
"shippingType": "Free"
},
"title": "Seismic Sports Slam Ball 10 - 30 lb Slam Ball for Crossfit, HIIT, Plyometrics",
"user_args": {
"advanced": null,
"pages": {
"entries_per_page": 10,
"page_number": 10
},
"search_terms": "ball"
}
},
"4": {
"country": "US",
"itemId": "113179929571",
"price": "20.89",
"shippingCost": {
"expeditedShipping": "true",
"handlingTime": "1",
"oneDayShippingAvailable": "true",
"shipToLocations": "Worldwide",
"shippingServiceCost": {
"_currencyId": "USD",
"value": "0.0"
},
"shippingType": "Free"
},
"title": "3 In 1 Kids Baby Play Tent Ball Pit Pool House Crawl Tunnel Indoor Outdoor Game",
"user_args": {
"advanced": null,
"pages": {
"entries_per_page": 10,
"page_number": 10
},
"search_terms": "ball"
}
},
"5": {
"country": "US",
"itemId": "153249589978",
"price": "10.34",
"shippingCost": {
"expeditedShipping": "true",
"handlingTime": "1",
"oneDayShippingAvailable": "false",
"shipToLocations": "Worldwide",
"shippingServiceCost": {
"_currencyId": "USD",
"value": "0.0"
},
"shippingType": "Free"
},
"title": "Premium Official Size 5 USA Soccer Ball W/ Pump Assorted Graphics!",
"user_args": {
"advanced": null,
"pages": {
"entries_per_page": 10,
"page_number": 10
},
"search_terms": "ball"
}
},
"6": {
"country": "US",
"itemId": "153168623537",
"price": "58.99",
"shippingCost": {
"expeditedShipping": "true",
"handlingTime": "1",
"oneDayShippingAvailable": "false",
"shipToLocations": "Worldwide",
"shippingServiceCost": {
"_currencyId": "USD",
"value": "0.0"
},
"shippingType": "Free"
},
"title": "Dragon Ball The Complete Series Seasons 1-5 - 1,2,3,4,5 New",
"user_args": {
"advanced": null,
"pages": {
"entries_per_page": 10,
"page_number": 10
},
"search_terms": "ball"
}
},
"7": {
"country": "US",
"itemId": "110874290750",
"price": "9.41",
"shippingCost": {
"expeditedShipping": "true",
"handlingTime": "1",
"oneDayShippingAvailable": "true",
"shipToLocations": "Worldwide",
"shippingServiceCost": {
"_currencyId": "USD",
"value": "0.0"
},
"shippingType": "FlatDomesticCalculatedInternational"
},
"title": "5006 Flexfit Sweep Low Profile Fitted Baseball Blank Plain Hat Ball Cap Flex Fit",
"user_args": {
"advanced": null,
"pages": {
"entries_per_page": 10,
"page_number": 10
},
"search_terms": "ball"
}
},
"8": {
"country": "US",
"itemId": "332908229449",
"price": "39.99",
"shippingCost": {
"expeditedShipping": "true",
"handlingTime": "2",
"oneDayShippingAvailable": "false",
"shipToLocations": "Worldwide",
"shippingServiceCost": {
"_currencyId": "USD",
"value": "0.0"
},
"shippingType": "Free"
},
"title": "ADIDAS BRAZUCA OFFICIAL MATCH BALL AUTHENTIC WORLD CUP 2014",
"user_args": {
"advanced": null,
"pages": {
"entries_per_page": 10,
"page_number": 10
},
"search_terms": "ball"
}
},
"9": {
"country": "US",
"itemId": "282781534125",
"price": "13.99",
"shippingCost": {
"expeditedShipping": "true",
"handlingTime": "1",
"oneDayShippingAvailable": "false",
"shipToLocations": "Worldwide",
"shippingServiceCost": {
"_currencyId": "USD",
"value": "0.0"
},
"shippingType": "Free"
},
"title": "3/4/5/6/8inch 110V Magic Crystal Globe Desktop Lightning Lamp Plasma Ball Sphere",
"user_args": {
"advanced": null,
"pages": {
"entries_per_page": 10,
"page_number": 10
},
"search_terms": "ball"
}
}
}
I do not know why I am receiving this undefined error, as I made sure to return response.Object for the ajaxResponse. Any help would be greatly appreciated!
There are a couple of issue here
Firstly you should generally be returning an array of objects not an object with enumerated properties.
instead of this:
{
{
"0": {
"country": "US",
},
"1": {
"country": "CN",
}
}
It should be this:
[
{"country": "US"},
{"country": "CN"},
]
At that point you wouldn't need to use the ajaxResponse function, tabulator would be able to parse the data directly.
If you can only return the data in that original format there is no problem, but it brings us onto the second issue, the reason you are getting the "undefined error" is because there is no "Object" property on the response object, only the properties "0", "1", "2" etc...
To convert your response to something that Tabulator can understand you will need to use the following ajaxResponse callback:
ajaxResponse:function(url, params, response){
return Object.values(response);
}
That will convert your returned object of objects into an array of objects.

Typescript model class for JSON object

I have the following JSON:
{
"-KtDGS8GdOcJ33Lcqjok": {
"2017": {
"address": "test address 1",
"area2": "3212",
"author": "STOkzlbT4OeOcbO2ed9Z7dvxZk92",
"category": "Solar",
"client": "Test Contact",
"createdAt": 1504551753483,
"lastEdited": 1504551805648,
"lastEditedBy": "STOkzlbT4OeOcbO2ed9Z7dvxZk92",
"lat": "18.490362758827665",
"lng": "-69.93279173970221",
"name": "17002 - test",
"pictures": {
"a95ff256-0f05-3122-a6b5-a88d3fd14c3f": true
},
"price": "213",
"province": "-KtDBavhyLhrpV8hDuj2",
"sector": "-KtDBqgy3CqpTv6c_iQ9",
"totalPrice": "1234",
"year": "2017"
}
},
"-KtDGaU9BB6eNj-MsyBg": {
"2015": {
"address": "test 2a",
"area1": "3245",
"author": "STOkzlbT4OeOcbO2ed9Z7dvxZk92",
"category": "Local: comercio",
"client": "test2 ",
"createdAt": 1504552100747,
"lastEdited": 1504552100747,
"lastEditedBy": "STOkzlbT4OeOcbO2ed9Z7dvxZk92",
"lat": "18.489417118875462",
"level": "4",
"lng": "-69.92930956184864",
"name": "15002 - test2a ",
"parking": "12",
"plaza": "Agora",
"price": "213",
"province": "-KtDBavhyLhrpV8hDuj2",
"restrooms": "2",
"sector": "-KtDBqgy3CqpTv6c_iQ9",
"totalPrice": "213",
"year": "2015"
},
"2017": {
"address": "test 2",
"area1": "3245",
"author": "STOkzlbT4OeOcbO2ed9Z7dvxZk92",
"category": "Local: comercio",
"client": "test2 ",
"createdAt": 1504551790632,
"lastEdited": 1504551790632,
"lastEditedBy": "STOkzlbT4OeOcbO2ed9Z7dvxZk92",
"lat": "18.489417118875462",
"level": "4",
"lng": "-69.92930956184864",
"name": "17003 - test2b",
"parking": "12",
"plaza": "Agora",
"price": "213",
"province": "-KtDBavhyLhrpV8hDuj2",
"restrooms": "2",
"sector": "-KtDBqgy3CqpTv6c_iQ9",
"totalPrice": "213",
"year": "2017"
}
},
"codeCounter": {
"2015": 2,
"2017": 5
},
"totals": {
"2015": 1,
"2017": 5
}
}
It's basically a list of objects, each object contains one or more nested objects wrapped in the year they were made (physically these are houses,apartments, etc.).
Where I'm having trouble is trying to map the 'year' objects (2017, 2015, etc') as they may or may not exist. For example, an object may have both 2017, 2016 entries or only one of them, etc.
I already have a 'Property' model class that I believe works, it has all the address, author, category, etc. fields. I'm trying to create the outter class that would contain a list of these property objects:
export interface PropertyWrapper {
[year: number]: Property;
}
I tried parsing the JSON as a PropertyWrapper[] array and this way I can already access a property by calling:
for (const pw of data) {
console.log(pw[2017]);
}
But this only works because I already know the object has a '2017' entry. How could I do this dynamically regardless of whether or not there's a '2017', '2010', or 10 entries ?
Are you looking for something like this.
First gets object keys, then loop through these keys and then loop through nested object keys
var jso = {
"-KtDGS8GdOcJ33Lcqjok": {
"2017": {
"address": "test address 1",
"area2": "3212",
"author": "STOkzlbT4OeOcbO2ed9Z7dvxZk92",
"category": "Solar",
"client": "Test Contact",
"createdAt": 1504551753483,
"lastEdited": 1504551805648,
"lastEditedBy": "STOkzlbT4OeOcbO2ed9Z7dvxZk92",
"lat": "18.490362758827665",
"lng": "-69.93279173970221",
"name": "17002 - test",
"pictures": {
"a95ff256-0f05-3122-a6b5-a88d3fd14c3f": true
},
"price": "213",
"province": "-KtDBavhyLhrpV8hDuj2",
"sector": "-KtDBqgy3CqpTv6c_iQ9",
"totalPrice": "1234",
"year": "2017"
}
},
"-KtDGaU9BB6eNj-MsyBg": {
"2015": {
"address": "test 2a",
"area1": "3245",
"author": "STOkzlbT4OeOcbO2ed9Z7dvxZk92",
"category": "Local: comercio",
"client": "test2 ",
"createdAt": 1504552100747,
"lastEdited": 1504552100747,
"lastEditedBy": "STOkzlbT4OeOcbO2ed9Z7dvxZk92",
"lat": "18.489417118875462",
"level": "4",
"lng": "-69.92930956184864",
"name": "15002 - test2a ",
"parking": "12",
"plaza": "Agora",
"price": "213",
"province": "-KtDBavhyLhrpV8hDuj2",
"restrooms": "2",
"sector": "-KtDBqgy3CqpTv6c_iQ9",
"totalPrice": "213",
"year": "2015"
},
"2017": {
"address": "test 2",
"area1": "3245",
"author": "STOkzlbT4OeOcbO2ed9Z7dvxZk92",
"category": "Local: comercio",
"client": "test2 ",
"createdAt": 1504551790632,
"lastEdited": 1504551790632,
"lastEditedBy": "STOkzlbT4OeOcbO2ed9Z7dvxZk92",
"lat": "18.489417118875462",
"level": "4",
"lng": "-69.92930956184864",
"name": "17003 - test2b",
"parking": "12",
"plaza": "Agora",
"price": "213",
"province": "-KtDBavhyLhrpV8hDuj2",
"restrooms": "2",
"sector": "-KtDBqgy3CqpTv6c_iQ9",
"totalPrice": "213",
"year": "2017"
}
},
"codeCounter": {
"2015": 2,
"2017": 5
},
"totals": {
"2015": 1,
"2017": 5
}
};
Object.keys(jso).forEach(function(key) {
Object.keys(jso[key]).forEach(function(nestedKey){
console.log(`nestedKey: ${nestedKey} \n ` , jso[key][nestedKey]);
});
});

validation of my code json

I am developping a simple application in AngularJs in the first time I create a script js but later I need to change it to json file so I need to validate this code json :
[{
"type": "line",
"plotarea": {
"adjust-layout":true /* For automatic margin adjustment. */
},
"scale-x": {
"label":{ /* Add a scale title with a label object. */
"text":"échelle essence gazoile",
},
/* Add your scale labels with a labels array. */
"labels":["sub01","sub02","sub02"]
},
"series": [
{"values":[1,8,1]},//here the prices of city selected
{"values":[14,13,14]}//here the qte of city selected
],
"name": "city A",
"elements": [{
"id": "c01",
"name": "name1",
"price": "15",
"qte": "10"
}, {
"id": "c02",
"name": "name2',
"price": "18,
"qte": "11"
}, {
"id": "c03",
"name": "name3",
"price": "11",
"qte": "14"
}],
"subsities": [{
"name": "sub A1",
"elements": [{
"id": "sub01",
"name": "nameSub1",
"price": "1",
"qte": "14"
}, {
"id": "sub02",
"name": "nameSub2",
"price": "8",
"qte": "13"
}, {
"id": "sub03",
"name": "nameSub3",
"price": "1",
"qte": "14"
}]
}, {
"name": "sub A2",
"elements": [{
"id": "ssub01",
"name": "nameSsub1",
"price": "1",
"qte": "7"
}, {
"id": "ssub02",
"name": "nameSsub2",
"price": "8",
"qte": "1"
}, {
"id": "ssub03",
"name": "nameSsub3",
"price": "4",
"qte": "19"
}]
}, {
"name": "sub A3",
"elements": [{
"id": "sssub01",
"name": "nameSssub1",
"price": "1",
"qte": "11"
}, {
"id": "sssub02",
"name": "nameSssub2",
"price": "2",
"qte": "15"
}, {
"id": "sssub03",
"name": "nameSssub3",
"price": "1",
"qte": "15"
}]
}]
}, {
"name": "city B",
"elements": [{
"id": "cc01",
"name": "name11",
"price": "10",
"qte": "11"
}, {
"id": "cc02",
"name": "name22",
"price": "14",
"qte": "19"
}, {
"id": "cc03",
"name": "name33",
"price": "11",
"qte": "18"
}]
}, {
"name": "city C",
"elements": [{
"id": "ccc01",
"name": "name111",
"price": "19",
"qte": "12"
}, {
"id": "ccc02",
"name": "name222",
"price": "18",
"qte": "17"
}, {
"id": "ccc03",
"name": "name333",
"price": "10",
"qte": "5"
}]
}];
A JSON Validator tells me that my code json is not correct.
Please anybody could help me !
The problem with comments and some of values doesn't contain , and some contains in the last value.(For example : 'json': {
'value1': 14,
'value2':14, // , is not allowed in the last line
}) Also last line can't contain ; after }]
Use this for validation jsonlint
This is correct json:
[{
"type": "line",
"plotarea": {
"adjust-layout": true
},
"scale-x": {
"label": {
"text": "échelle essence gazoile"
},
"labels": ["sub01", "sub02", "sub02"]
},
"series": [{
"values": [1, 8, 1]
}, {
"values": [14, 13, 14]
}],
"name": "city A",
"elements": [{
"id": "c01",
"name": "name1",
"price": "15",
"qte": "10"
}, {
"id": "c02",
"name": "name2",
"price": "18",
"qte": "11"
}, {
"id": "c03",
"name": "name3",
"price": "11",
"qte": "14"
}],
"subsities": [{
"name": "sub A1",
"elements": [{
"id": "sub01",
"name": "nameSub1",
"price": "1",
"qte": "14"
}, {
"id": "sub02",
"name": "nameSub2",
"price": "8",
"qte": "13"
}, {
"id": "sub03",
"name": "nameSub3",
"price": "1",
"qte": "14"
}]
}, {
"name": "sub A2",
"elements": [{
"id": "ssub01",
"name": "nameSsub1",
"price": "1",
"qte": "7"
}, {
"id": "ssub02",
"name": "nameSsub2",
"price": "8",
"qte": "1"
}, {
"id": "ssub03",
"name": "nameSsub3",
"price": "4",
"qte": "19"
}]
}, {
"name": "sub A3",
"elements": [{
"id": "sssub01",
"name": "nameSssub1",
"price": "1",
"qte": "11"
}, {
"id": "sssub02",
"name": "nameSssub2",
"price": "2",
"qte": "15"
}, {
"id": "sssub03",
"name": "nameSssub3",
"price": "1",
"qte": "15"
}]
}]
}, {
"name": "city B",
"elements": [{
"id": "cc01",
"name": "name11",
"price": "10",
"qte": "11"
}, {
"id": "cc02",
"name": "name22",
"price": "14",
"qte": "19"
}, {
"id": "cc03",
"name": "name33",
"price": "11",
"qte": "18"
}]
}, {
"name": "city C",
"elements": [{
"id": "ccc01",
"name": "name111",
"price": "19",
"qte": "12"
}, {
"id": "ccc02",
"name": "name222",
"price": "18",
"qte": "17"
}, {
"id": "ccc03",
"name": "name333",
"price": "10",
"qte": "5"
}]
}]
the problem is that you have comments in your JSON. This is not allowed in pure json.
In addition you have some syntax errors:
Line 8 :"text": "échelle essence gazoile", the , has to be removed because it is the last property of the object
Line 26: "name": "name2', change the singlequote to a doublequote
Line 27: "price": "18,: add a doublequote at the end
Last line: }]; remove the semicolon
This is your valid json:
[{
"type": "line",
"plotarea": {
"adjust-layout": true
},
"scale-x": {
"label": {
"text": "échelle essence gazoile"
},
"labels": ["sub01", "sub02", "sub02"]
},
"series": [{
"values": [1, 8, 1]
}, {
"values": [14, 13, 14]
}],
"name": "city A",
"elements": [{
"id": "c01",
"name": "name1",
"price": "15",
"qte": "10"
}, {
"id": "c02",
"name": "name2",
"price": "18",
"qte": "11"
}, {
"id": "c03",
"name": "name3",
"price": "11",
"qte": "14"
}],
"subsities": [{
"name": "sub A1",
"elements": [{
"id": "sub01",
"name": "nameSub1",
"price": "1",
"qte": "14"
}, {
"id": "sub02",
"name": "nameSub2",
"price": "8",
"qte": "13"
}, {
"id": "sub03",
"name": "nameSub3",
"price": "1",
"qte": "14"
}]
}, {
"name": "sub A2",
"elements": [{
"id": "ssub01",
"name": "nameSsub1",
"price": "1",
"qte": "7"
}, {
"id": "ssub02",
"name": "nameSsub2",
"price": "8",
"qte": "1"
}, {
"id": "ssub03",
"name": "nameSsub3",
"price": "4",
"qte": "19"
}]
}, {
"name": "sub A3",
"elements": [{
"id": "sssub01",
"name": "nameSssub1",
"price": "1",
"qte": "11"
}, {
"id": "sssub02",
"name": "nameSssub2",
"price": "2",
"qte": "15"
}, {
"id": "sssub03",
"name": "nameSssub3",
"price": "1",
"qte": "15"
}]
}]
}, {
"name": "city B",
"elements": [{
"id": "cc01",
"name": "name11",
"price": "10",
"qte": "11"
}, {
"id": "cc02",
"name": "name22",
"price": "14",
"qte": "19"
}, {
"id": "cc03",
"name": "name33",
"price": "11",
"qte": "18"
}]
}, {
"name": "city C",
"elements": [{
"id": "ccc01",
"name": "name111",
"price": "19",
"qte": "12"
}, {
"id": "ccc02",
"name": "name222",
"price": "18",
"qte": "17"
}, {
"id": "ccc03",
"name": "name333",
"price": "10",
"qte": "5"
}]
}]
You cannot have a C Style or C# style comments inside a json document.As a matter of fact you cannot have any comments inside a json document
You can validate your json document on http://jsonlint.com/
You can try this# http://jsonlint.com/
this will validate your json and also gives you error where you are doing mistakes

Remove element from JSON array recursively

I have the following json array:
{
"id": "1004482725",
"parent": 0,
"industry": "Akrotiri",
"industry_id": "root",
"name": "Comp",
"percentage": "0",
"report": "null",
"description": "",
"subsidiary": "3",
"children": [{
"id": "1004482733",
"parent": "1004482725",
"industry": "Akrotiri",
"industry_id": "industry9",
"name": "Comp11",
"percentage": "30",
"report": "null",
"description": "",
"subsidiary": "1",
"children": [{
"id": "1004482734",
"parent": "1004482733",
"industry": "Akrotiri",
"industry_id": "industry10",
"name": "Comp110",
"percentage": "30.00",
"report": "null",
"description": "",
"subsidiary": "1",
"children": [{
"id": "aa02634",
"parent": "1004482734",
"industry": "",
"industry_id": "0",
"name": "Sef 4",
"percentage": "4",
"report": "null",
"description": "",
"subsidiary": "0",
"children": []
}]
}, {
"id": "1004482735",
"parent": "1004482733",
"industry": "Akrotiri",
"industry_id": "industry11",
"name": "Comp111",
"percentage": "30.00",
"report": "null",
"description": "",
"subsidiary": "1",
"children": [{
"id": "aa02635",
"parent": "1004482735",
"industry": "",
"industry_id": "0",
"name": "Sef 5",
"percentage": "5",
"report": "null",
"description": "",
"subsidiary": "0",
"children": []
}]
}, {
"id": "1004482736",
"parent": "1004482733",
"industry": "Akrotiri",
"industry_id": "industry12",
"name": "Comp112",
"percentage": "30.00",
"report": "null",
"description": "",
"subsidiary": "1",
"children": [{
"id": "1004482737",
"parent": "1004482736",
"industry": "Akrotiri",
"industry_id": "industry13",
"name": "Comp1120",
"percentage": "100.00",
"report": "null",
"description": "",
"subsidiary": "2",
"children": [{
"id": "1004482738",
"parent": "1004482737",
"industry": "Akrotiri",
"industry_id": "industry14",
"name": "Comp11200",
"percentage": "100.00",
"report": "null",
"description": "",
"subsidiary": "1",
"children": [{
"id": "1004482739",
"parent": "1004482738",
"industry": "Akrotiri",
"industry_id": "industry15",
"name": "Comp112000",
"percentage": "100.00",
"report": "null",
"description": "",
"subsidiary": "1",
"children": [{
"id": "aa02636",
"parent": "1004482739",
"industry": "",
"industry_id": "0",
"name": "Sef 6",
"percentage": "6",
"report": "null",
"description": "",
"subsidiary": "0",
"children": []
}]
}]
}]
}, {
"id": "1004482740",
"parent": "1004482736",
"industry": "Akrotiri",
"industry_id": "industry16",
"name": "Comp1121",
"percentage": "100.00",
"report": "null",
"description": "",
"subsidiary": "1",
"children": [{
"id": "1004482741",
"parent": "1004482740",
"industry": "Akrotiri",
"industry_id": "industry17",
"name": "Comp11210",
"percentage": "100.00",
"report": "null",
"description": "",
"subsidiary": "1",
"children": [{
"id": "aa02637",
"parent": "1004482741",
"industry": "",
"industry_id": "0",
"name": "Sef 7",
"percentage": "7",
"report": "null",
"description": "",
"subsidiary": "0",
"children": []
}]
}]
}, {
"id": "1004482742",
"parent": "1004482736",
"industry": "Akrotiri",
"industry_id": "industry18",
"name": "Comp1122",
"percentage": "30.00",
"report": "null",
"description": "",
"subsidiary": "2",
"children": [{
"id": "1004482743",
"parent": "1004482742",
"industry": "Akrotiri",
"industry_id": "industry19",
"name": "Comp11220",
"percentage": "100.00",
"report": "null",
"description": "",
"subsidiary": "1",
"children": [{
"id": "aa02638",
"parent": "1004482743",
"industry": "",
"industry_id": "0",
"name": "Sef 8",
"percentage": "8",
"report": "null",
"description": "",
"subsidiary": "0",
"children": []
}]
}]
}]
}]
}, {
"id": "1004482726",
"parent": "1004482725",
"industry": "Akrotiri",
"industry_id": "industry2",
"name": "Comp10",
"percentage": "30",
"report": "null",
"description": "",
"subsidiary": "1",
"children": [{
"id": "1004482732",
"parent": "1004482726",
"industry": "Akrotiri",
"industry_id": "industry8",
"name": "Comp102",
"percentage": "30",
"report": "null",
"description": "",
"subsidiary": "1",
"children": [{
"id": "aa02633",
"parent": "1004482732",
"industry": "",
"industry_id": "0",
"name": "Sef 3",
"percentage": "3",
"report": "null",
"description": "",
"subsidiary": "0",
"children": []
}]
}, {
"id": "1004482728",
"parent": "1004482726",
"industry": "Akrotiri",
"industry_id": "industry4",
"name": "Comp101",
"percentage": "30",
"report": "null",
"description": "",
"subsidiary": "1",
"children": [{
"id": "1004482729",
"parent": "1004482728",
"industry": "Akrotiri",
"industry_id": "industry5",
"name": "Comp1010",
"percentage": "100.00",
"report": "null",
"description": "",
"subsidiary": "1",
"children": [{
"id": "1004482730",
"parent": "1004482729",
"industry": "Akrotiri",
"industry_id": "industry6",
"name": "Comp10100",
"percentage": "100.00",
"report": "null",
"description": "",
"subsidiary": "1",
"children": [{
"id": "1004482731",
"parent": "1004482730",
"industry": "Akrotiri",
"industry_id": "industry7",
"name": "Comp101000",
"percentage": "100.00",
"report": "null",
"description": "",
"subsidiary": "1",
"children": [{
"id": "aa02632",
"parent": "1004482731",
"industry": "",
"industry_id": "0",
"name": "Sef 2",
"percentage": "2",
"report": "null",
"description": "",
"subsidiary": "0",
"children": []
}]
}]
}]
}]
}, {
"id": "1004482727",
"parent": "1004482726",
"industry": "Akrotiri",
"industry_id": "industry3",
"name": "Comp100",
"percentage": "30",
"report": "null",
"description": "",
"subsidiary": "1",
"children": [{
"id": "aa02631",
"parent": "1004482727",
"industry": "",
"industry_id": "0",
"name": "Sef1",
"percentage": "1",
"report": "null",
"description": "",
"subsidiary": "0",
"children": []
}]
}]
}, {
"id": "1004482744",
"parent": "1004482725",
"industry": "Akrotiri",
"industry_id": "industry20",
"name": "Comp12",
"percentage": "30",
"report": "null",
"description": "",
"subsidiary": "1",
"children": [{
"id": "1004482745",
"parent": "1004482744",
"industry": "Akrotiri",
"industry_id": "industry21",
"name": "Comp120",
"percentage": "30.00",
"report": "null",
"description": "",
"subsidiary": "1",
"children": [{
"id": "",
"parent": "",
"industry": "",
"industry_id": "",
"name": "",
"percentage": "",
"report": "",
"description": "",
"subsidiary": "",
"children": ""
}, {
"id": "1004482748",
"parent": "1004482745",
"industry": "Akrotiri",
"industry_id": "industry24",
"name": "Comp1210",
"percentage": "50.00",
"report": "null",
"description": "",
"subsidiary": "2",
"children": [{
"id": "1004482749",
"parent": "1004482748",
"industry": "Akrotiri",
"industry_id": "industry25",
"name": "Comp12100",
"percentage": "100.00",
"report": "null",
"description": "",
"subsidiary": "1",
"children": [{
"id": "1004482750",
"parent": "1004482749",
"industry": "Akrotiri",
"industry_id": "industry26",
"name": "Comp121000",
"percentage": "100.00",
"report": "null",
"description": "",
"subsidiary": "1",
"children": [{
"id": "aa02639",
"parent": "1004482750",
"industry": "",
"industry_id": "0",
"name": "Sef 9",
"percentage": "9",
"report": "null",
"description": "",
"subsidiary": "0",
"children": []
}]
}]
}]
}, {
"id": "",
"parent": "",
"industry": "",
"industry_id": "",
"name": "",
"percentage": "",
"report": "",
"description": "",
"subsidiary": "",
"children": ""
}]
}, {
"id": "1004482746",
"parent": "1004482744",
"industry": "Akrotiri",
"industry_id": "industry22",
"name": "Comp121",
"percentage": "30.00",
"report": "null",
"description": "",
"subsidiary": "1",
"children": [{
"id": "",
"parent": "",
"industry": "",
"industry_id": "",
"name": "",
"percentage": "",
"report": "",
"description": "",
"subsidiary": "",
"children": ""
}]
}, {
"id": "1004482747",
"parent": "1004482744",
"industry": "Akrotiri",
"industry_id": "industry23",
"name": "Comp122",
"percentage": "30.00",
"report": "null",
"description": "",
"subsidiary": "1",
"children": [{
"id": "aa02640",
"parent": "1004482747",
"industry": "",
"industry_id": "0",
"name": "Sef 10",
"percentage": "10",
"report": "null",
"description": "",
"subsidiary": "0",
"children": []
}]
}]
}]
}
In this example i have some elements that looks like this:
"children": [{
"id": "",
"parent": "",
"industry": "",
"industry_id": "",
"name": "",
"percentage": "",
"report": "",
"description": "",
"subsidiary": "",
"children": ""
}
What i would like to achive is to eliminate this "childrens" from the original array.
So far i've tried this function but it doesn't work the way i want:
function deleteEmpty(data){
if (data.children.length)
{
nr = data.children.length;
for (i=0;i<nr;i++)
{
// console.log(data.children[i]);
if (data.children[i]["id"] == ""){
console.log(data.children[i]);
delete data.children[i];
}
this.deleteEmpty(data.children[i]);
}
}
return data;
}
Also i have creatd a jsfiddle here : http://jsfiddle.net/fP7NS/2/
Please help me out or give me some hints with what i am doing wrong.
First (if necessary) turn your JSON string into an object. Your children property is sometimes an array, sometimes an empty array, and sometimes an empty string. This makes the logic a bit awkward. i suggest you write a function that tests whether a particular child is empty. Then you can write your main loop like this:
function isEmpty(node) {
// logic for testing that a node is empty
}
function deleteEmptyChildren(node) {
// assumes node is not empty
if (Array.isArray(node.children)) {
node.children = node.children.filter(
function(child) {
if (isEmpty(child)) {
return false;
} else {
deleteEmptyChildren(child);
return true;
}
}
);
}
}

Categories

Resources