validation of my code json - javascript

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

Related

How can I accurately return nested values from JSON for comparison and then complete the comparison?

I have asked a similar question before but I've not been able to expand on this and I haven't found the exact answer that would teach me how to do this. My JSON file will have the same structure in terms of the element names inside each nested object but the values will vary. In plain English, I want to start at the bottom of the file and return true if the following conditions occur and stop searching if true: AppStatus is one of these ["Approved", "Auto Approved", "Return"] and the nested keys of "Name": "Payments", "Value": "X" and "Name": "Term", "Value": "Y" have values that don't match. I'm providing a snippet of the JSON so you can see more easily. The AppStatus is 2nd level and the Name, Value are 3rd level. In this example, it should return true based on the 2nd set of data in DataElements.
{
"DecisionHistory": [
{
"Id": "273601",
"Number": "1",
"CreateDate": "2022-10-13 15:31:18.683",
"AppStatus": "Approved",
"DataElements": [
{
"Id": "213922",
"Name": "Payments",
"Value": "72",
"GroupId": "12",
"CustomLabel": null
},
{
"Id": "990",
"Name": "Decisioned By",
"Value": "ASDF",
"GroupId": "3",
"CustomLabel": null
},
{
"Id": "215337",
"Name": "Term",
"Value": "75",
"GroupId": "13",
"CustomLabel": null
}
]
},
{
"Id": "273601",
"Number": "2",
"CreateDate": "2022-10-13 15:31:18.683",
"AppStatus": "Approved",
"DataElements": [
{
"Id": "213922",
"Name": "Payments",
"Value": "72",
"GroupId": "12",
"CustomLabel": null
},
{
"Id": "990",
"Name": "Decisioned By",
"Value": "ASDF",
"GroupId": "3",
"CustomLabel": null
},
{
"Id": "215337",
"Name": "Term",
"Value": "75",
"GroupId": "13",
"CustomLabel": null
}
]
},
{
"Id": "273601",
"Number": "3",
"CreateDate": "2022-10-13 15:31:18.683",
"AppStatus": "Approved",
"DataElements": [
{
"Id": "213922",
"Name": "Payments",
"Value": "75",
"GroupId": "12",
"CustomLabel": null
},
{
"Id": "990",
"Name": "Decisioned By",
"Value": "ASDF",
"GroupId": "3",
"CustomLabel": null
},
{
"Id": "215337",
"Name": "Term",
"Value": "75",
"GroupId": "13",
"CustomLabel": null
}
]
}
]
}
I've tried using 2 ugly nested for loops without success. I would prefer not to use for loops if I don't have to. See below:
function main(jsonFile) {
const history = JSON.parse(jsonFile).DecisionHistory;
const appStatus = ["Approved", "Auto Approved", "Return"];
const termMonths = "TermMonths";
const numberOfPayments = "Number of Payments";
let termMonthsVal = 0;
let numberofPaymentsVal = 0;
for (let a = history.length - 1; a >= 0; a--) {
if (appStatus.includes(history[a].AppStatus)) {
var dataElementsA = history[a].DataElements;
for (let b = (dataElementsA.length) - 1; b >= 0; b--) {
if (dataElementsA[b].Name == termMonths) {
termMonthsVal = new Number((dataElementsA[b].Value));
break;
}
}
}
}
for (let a = history.length - 1; a >= 0; a--) {
if (appStatus.includes(history[a].AppStatus)) {
var dataElementsB = history[a].DataElements;
for (let b = (dataElementsB.length) - 1; b >= 0; b--) {
if (dataElementsB[b].Name == numberOfPayments) {
numberofPaymentsVal = new Number((dataElementsB[b].Value));
break;
}
}
}
}
return (termMonthsVal != numberofPaymentsVal);
}
let jsonFile = `{
"DecisionHistory": [{
"Id": "273601",
"Number": "1",
"CreateDate": "2022-10-13 15:31:18.683",
"AppStatus": "Approved",
"DataElements": [{
"Id": "213922",
"Name": "Payments",
"Value": "72",
"GroupId": "12",
"CustomLabel": null
},
{
"Id": "990",
"Name": "Decisioned By",
"Value": "ASDF",
"GroupId": "3",
"CustomLabel": null
},
{
"Id": "215337",
"Name": "Term",
"Value": "75",
"GroupId": "13",
"CustomLabel": null
}
]
},
{
"Id": "273601",
"Number": "2",
"CreateDate": "2022-10-13 15:31:18.683",
"AppStatus": "Approved",
"DataElements": [{
"Id": "213922",
"Name": "Payments",
"Value": "72",
"GroupId": "12",
"CustomLabel": null
},
{
"Id": "990",
"Name": "Decisioned By",
"Value": "ASDF",
"GroupId": "3",
"CustomLabel": null
},
{
"Id": "215337",
"Name": "Term",
"Value": "75",
"GroupId": "13",
"CustomLabel": null
}
]
},
{
"Id": "273601",
"Number": "3",
"CreateDate": "2022-10-13 15:31:18.683",
"AppStatus": "Approved",
"DataElements": [{
"Id": "213922",
"Name": "Payments",
"Value": "75",
"GroupId": "12",
"CustomLabel": null
},
{
"Id": "990",
"Name": "Decisioned By",
"Value": "ASDF",
"GroupId": "3",
"CustomLabel": null
},
{
"Id": "215337",
"Name": "Term",
"Value": "75",
"GroupId": "13",
"CustomLabel": null
}
]
}
]
}`;
console.log(main(jsonFile));
//var result = main("_inJson");
//module.exports = main;
As a side note, the jsonFile in the main function is read in from a resource folder in my VS Code and I'm exporting main to an app.js so I can run assertions against it. So the JSON posted here is actually in a separate file but this is the exact structure of the data.
What am I missing? .filter? .map? .reduce? Differnt breaks?
Here's a "one-liner" depending on how long you want your lines to be ;)
const x = {
"DecisionHistory": [
{
"AppStatus": "Approved",
"DataElements": [
{
"Name": "Payments",
"Value": "72",
},
{
"Name": "Decisioned By",
"Value": "ASDF",
},
{
"Name": "Term",
"Value": "75",
}
]
},
{
"AppStatus": "Approved",
"DataElements": [
{
"Name": "Payments",
"Value": "72",
},
{
"Name": "Decisioned By",
"Value": "ASDF",
},
{
"Name": "Term",
"Value": "75",
}
]
},
{
"AppStatus": "Approved",
"DataElements": [
{
"Name": "Payments",
"Value": "75",
},
{
"Name": "Decisioned By",
"Value": "ASDF",
},
{
"Name": "Term",
"Value": "75",
}
]
}
]
};
const bar = (f) =>
f.DecisionHistory.reverse().some((decision) => ["Approved", "Auto Approved", "Return"].includes(decision.AppStatus) && decision.DataElements.find((el) => el.Name === 'Payments')?.Value !== decision.DataElements.find((el) => el.Name === 'Term')?.Value);
console.log(bar(x));

How can a JSON call another JSON

I am developing a simple application with AngularJS and I want to get the value of the price dynamically so I want to make a JSON call to another JSON and I am not sure if is this possible !
This is my data.json:
[{
"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"
}]
}]
And I want to get the value of price from this url : url json
I want to do something like :
[{
"name": "city A",
"elements": [{
"id": "c01",
"name": "name1",
"price": "prix_diesel",/*prix_diesel is an attribute in json url I want to extract its value here.*/
"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"
}]
}]
}
......
......
]
I set this up in my plunkr : plunkr
I search and I found that if I want to get the data from the URL, I'll need an XMLHttpRequest (with Angular, I can use $http.get). But here I am using a JSON which call another JSON file so how can I resolve this problem ? do you have any idea please !
You can't. JSON is not a programming language but an open standard for data transfer.
What you can do however is reference an object from the second file in the first file. An example:
[{
"name": "city A",
"elements": [{
"id": "c01",
"name": "name1",
"price": "prix_diesel",
"qte": "10"
}]
// Simplified
}]
And the JSON from your URL:
{
"prix": {
"prix_diesel": "8.25",
"prix_essence": "10.14",
"prix_aditive": "8.80"
}
}
Now write a JS function which finds the reference in your first JSON file prix_diesel and look up the value of that key in the JSON from the url 8.25.
An example would be similar to this:
for (var element of myJson.elements) {
var reference = element.price; // prix_diesel
var referenceValue = jsonFromUrl.prix[reference]; // 8.25
}
This will find all the prices of your elements based on their reference (which is actually just the name of a key in your second JSON).
A side note, I noticed the JSON from your URL is supposed to be protected by login functionality. However, like you demonstrate in your own post, with the correct endpoints the data is easily accessible without needing to log in. If this is a product you own and you don't want this data to be publically available I'd suggest looking into a solution to prevent non-logged in users from requesting data. Have a look at passportjs.org.
i forked your plunker (changed your data.json too)
after loading second json you can get it like(code is something like "prix_diesel". it is in updated json)
function getPrice(code){
return priceData.prix[code];
}
updated

Error when I try to parse my json

I am using AngularJs doing a simple application And I want to set a variable up in a javascript file in orther to call a json file.
This is my data.js :
[{
var data = require('data_old.json');
var json = JSON.parse(data);
alert(json["date"]); //01/05/2016
alert(json.date); //01/05/2016
"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"
}]
}]
And this is my data.json
[{
"date":"01/05/2016"
}]
I call my data here.
angular.module("myApp",['zingchart-angularjs'])
.controller('MainController', ['$scope', '$http', function($scope, $http) {
$scope.chartBase = {
"type": "line",
"plotarea": {
"adjust-layout": true /* For automatic margin adjustment. */
},
"scale-x": {
"label": {
"text": "Above is an example of a category scale" /* Add a scale title with a label object. */
},
"labels": ["name1", "name2", "name3"] /* Add your scale labels with a labels array. */
},
"series": [{
"values": [15, 18, 11] //here the prices of city selected
},{
"values": [10, 11, 14] //here the qte of city selected
}]
};
$scope.chartData = angular.copy($scope.chartBase);
$http.get('data.js')
.then(function(response) {
$scope.cities = response.data; // save the request data
$scope.selectedCity = $scope.cities[0]; // select the first one
$scope.changeCity(); // update chart
}, function(error) { console.log(error); });
$scope.changeCity = function() {
if($scope.selectedSubCity || $scope.selectedCity){ // if something has been selected
$scope.data = ($scope.selectedSubCity || $scope.selectedCity).elements; // update elements field
// initialize the array to be displayed in chart
var labels = [];
var price = {
"values": []
};
var qte = {
"values": []
};
// fill the arrays to be displayed from the selected city (sub city)
angular.forEach($scope.data, function(item, index) {
labels.push(item.name);
price.values.push(parseInt(item.price));
qte.values.push(parseInt(item.qte));
});
console.log($scope.chartData)
// put selected values to the field that is used to render the chart
$scope.chartData["scale-x"].labels = labels;
$scope.chartData.series = [ price, qte ];
}
}
}]);
When I run this, the browser tell that I have this error :
SyntaxError: Unexpected token v in JSON at position 5
at Object.parse (native)
You can't have those in your data.js file as they are not valid JSON
var data = require('data_old.json');
var json = JSON.parse(data);
alert(json["date"]); //01/05/2016
alert(json.date); //01/05/2016
The error is pointing to the first "v" of "var". You can test your JSON here http://json.parser.online.fr/ on some other online validator.
Your file should look like this:
[{
"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"
}]
}]
var data = require('data_old.json');
var json = JSON.parse(data);
alert(json["date"]); //01/05/2016
alert(json.date); //01/05/2016
Why do you add those at the beginning of the JSON ? They cannot be parsed by your script, thus displaying the Syntax Error. What do you want to do exactly ?

How to push data into an object?

I have object and I am trying to push data into it, but I am getting the error "push is not a function".
Here's my object:
var item = {
"_id": {
"$oid": "asdf976"
},
"Categories": [{
"mainmodels": [{
"submodels": [{
"price": "2000",
"submodelname": "lumia021",
"Remainingphones": "0",
"Bookedphones": "0",
"Numofphones": "10"
}, {
"price": "2000",
"submodelname": "lumia341",
"Remainingphones": "5",
"Bookedphones": "5",
"Numofphones": "10"
}
],
"Status": "Active",
"modelname": "lumia",
"fromdate": "2016-04-01T16:39:12.051Z",
"todate": "2016-04-31T19:19:44.051Z"
}],
"brand": "nokia"
}],
"rank": "1",
"name": "first"
}
I want to push:
var modal = {
'custid': '1',
'packcode': '22'
};
item.push(modal);
console.log(item);
Here's my expected result:
var item = {
"_id": {
"$oid": "asdf976"
},
"Categories": [{
"mainmodels": [{
"submodels": [{
"price": "2000",
"submodelname": "lumia021",
"Remainingphones": "0",
"Bookedphones": "0",
"Numofphones": "10"
}, {
"price": "2000",
"submodelname": "lumia341",
"Remainingphones": "5",
"Bookedphones": "5",
"Numofphones": "10"
}
],
"Status": "Active",
"modelname": "lumia",
"fromdate": "2016-04-01T16:39:12.051Z",
"todate": "2016-04-31T19:19:44.051Z"
}],
"brand": "nokia"
}],
"rank": "1",
"name": "first",
'custid': '1',
'packcode': '22'
};
How do I do that?
You don't push into objects, you push into arrays.
Based on what you've said you want, you do this:
item.custid = '1';
item.packcode = '22';
E.g., just assign properties to the object.
In ES2015, you could use Object.assign if having modal be separate is important:
var modal = {custid: '1', packcode: '22'};
Object.assign(item, modal);
...but I don't see a need for it here, and note that there's no ongoing link between modal and item.
Live Example:
var item = {
"_id": {
"$oid": "asdf976"
},
"Categories": [{
"mainmodels": [{
"submodels": [{
"price": "2000",
"submodelname": "lumia021",
"Remainingphones": "0",
"Bookedphones": "0",
"Numofphones": "10"
}, {
"price": "2000",
"submodelname": "lumia341",
"Remainingphones": "5",
"Bookedphones": "5",
"Numofphones": "10"
}
],
"Status": "Active",
"modelname": "lumia",
"fromdate": "2016-04-01T16:39:12.051Z",
"todate": "2016-04-31T19:19:44.051Z"
}],
"brand": "nokia"
}],
"rank": "1",
"name": "first"
};
item.custid = '1';
item.packcode = '22';
// Note: Just using JSON for *display*
document.body.innerHTML =
"<pre>" + JSON.stringify(item, null, 2) + "</pre>";
Or with Object.assign (requires an up-to-date browser or a shim):
var item = {
"_id": {
"$oid": "asdf976"
},
"Categories": [{
"mainmodels": [{
"submodels": [{
"price": "2000",
"submodelname": "lumia021",
"Remainingphones": "0",
"Bookedphones": "0",
"Numofphones": "10"
}, {
"price": "2000",
"submodelname": "lumia341",
"Remainingphones": "5",
"Bookedphones": "5",
"Numofphones": "10"
}
],
"Status": "Active",
"modelname": "lumia",
"fromdate": "2016-04-01T16:39:12.051Z",
"todate": "2016-04-31T19:19:44.051Z"
}],
"brand": "nokia"
}],
"rank": "1",
"name": "first"
};
var modal = {
custid: '1',
packcode: '22'
};
Object.assign(item, modal);
// Note: Just using JSON for *display*
document.body.innerHTML =
"<pre>" + JSON.stringify(item, null, 2) + "</pre>";

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