Json encode data formatting - json

Below part is my json encode data,
[
{
project_name: "Faithview 1 Residence",
BookedUnit: "25",
GDV: "9,143,860.00"
},
{
project_name: "Faithview 2 Residence",
BookedUnit: "24", GDV: "8,795,380.00"
},
{
project_name: "Faithview 3 Residence",
BookedUnit: "24",
GDV: "8,795,380.00"
}
]
I would like to change the json data become like this as shown below:-
[
{
project_name: "Faithview 1 Residence",
Unit: 25
},
{
project_name: "Faithview 2 Residence",
Unit: 24
},
{
project_name: "Faithview 3 Residence",
Unit: 24
}
]
There is 3 condition in the new json data which is :-
1) Delete the GDV
2) The key of BookedUnit change to Unit.
3) The format value of "Unit" now become Int.
Any suggestion and solution is appreciated.

Please check this solution.This is work for you
var oldJSON = [{project_name: "Faithview 1 Residence", BookedUnit: "25", GDV: "9,143,860.00"} , {project_name: "Faithview 2 Residence", BookedUnit: "24", GDV: "8,795,380.00"} , {project_name: "Faithview 3 Residence", BookedUnit: "24", GDV: "8,795,380.00"}];
var newJSON = [];
for(key in oldJSON){
newJSON[key] = {project_name : oldJSON[key].project_name, Unit : parseInt(oldJSON[key].BookedUnit)};
}
console.log(newJSON);

Related

I want to add an element to a list from a map in flutter

I have a list model class name previous loan. I want to add loanOutstanding value in previousLoan Model if its key has matched. How can I do this?
"prevLoans": [
{
"id": 200855,
"disbursementDate": "2018-10-09",
"lastInstallmentDate": "2022-01-10",
"loanCompletedDate": "0000-00-00",
"loanAmount": 70000
},{
"id": 345855,
"disbursementDate": "2018-10-09",
"lastInstallmentDate": "2022-01-10",
"loanCompletedDate": "0000-00-00",
"loanAmount": 70000
},{
"id": 965821,
"disbursementDate": "2018-10-09",
"lastInstallmentDate": "2022-01-10",
"loanCompletedDate": "0000-00-00",
"loanAmount": 70000
},
],
"loanOutstanding": {
"200855": 21379
},
as I found out from your explanation, and your Map, i added { } to your Map because your map is not complete in my idea:
var loansMap ={
"prevLoans": [
{
"id": 200855,
"disbursementDate": "2018-10-09",
"lastInstallmentDate": "2022-01-10",
"loanCompletedDate": "0000-00-00",
"loanAmount": 70000
},{
"id": 345855,
"disbursementDate": "2018-10-09",
"lastInstallmentDate": "2022-01-10",
"loanCompletedDate": "0000-00-00",
"loanAmount": 70000
},{
"id": 965821,
"disbursementDate": "2018-10-09",
"lastInstallmentDate": "2022-01-10",
"loanCompletedDate": "0000-00-00",
"loanAmount": 70000
},
],
"loanOutstanding": {
"200855": 21379
},
};
here is the code to put 21379 as a value of loanOutstanding key in prevLoans Model:
for (var loan in loansMap['prevLoans'] as List) {
final id = loan['id'].toString();
if ((loansMap['loanOutstanding'] as Map).containsKey(id)) {
loan['loanOutstanding'] = (loansMap['loanOutstanding'] as Map)[id];
}
}
print('loans: ${loansMap}');
here is the print result:
loans: {
prevLoans: [
{
id: 200855,
disbursementDate: 2018-10-09,
lastInstallmentDate: 2022-01-10,
loanCompletedDate: 0000-00-00,
loanAmount: 70000,
loanOutstanding: 21379
},
{
id: 345855,
disbursementDate: 2018-10-09,
lastInstallmentDate: 2022-01-10,
loanCompletedDate: 0000-00-00,
loanAmount: 70000
},
{
id: 965821,
disbursementDate: 2018-10-09,
lastInstallmentDate: 2022-01-10,
loanCompletedDate: 0000-00-00,
loanAmount: 70000
}
],
loanOutstanding: {200855: 21379}
}
as you see loanOutstanding: 21379 added.
if some of the structured changed is because you haven't added clear map in your explanation.
but if you have any question or help i'm here ;)
happy coding...
After parsing the json, you can iterate over the loan list and modify the matched one:
for (final loan in loans) {
final id = loan['id'].toString();
if (outstandings.containsKey(id)) {
loan['loanOutstanding'] = outstandings[id]!;
}
}

Change type of input from string to data, MONGO DB

i'm trying to convert the inspection_date field from string to date for every object inside my db.
Every object is built like this one.
"name": "$1 STORE",
"address": "5573 ROSEMEAD BLVD",
"city": "TEMPLE CITY",
"zipcode": "91780",
"state": "California",
"violations": [{
"inspection_date": "2015-09-29",
"description": " points ... violation_status\n62754 1 ... OUT OF COMPLIANCE\n62755 1 ... OUT OF COMPLIANCE\n62756 2 ... OUT OF COMPLIANCE\n\n[3 rows x 5 columns]",
"risk": "Risk 3 (Low)"
}, {
"inspection_date": "2016-08-18",
"description": " points ... violation_status\n338879 2 ... OUT OF COMPLIANCE\n\n[1 rows x 5 columns]",
"risk": "Risk 3 (Low)"
} //could be more than 2 or less then 2 object inside violations array//]}
How can i convert all of the inspection_date field avoiding doing it by myself one by one?
As suggested by #turivishal, you have to have to make use of $map and $dateFromString operators.
db.collection.aggregate([
{
"$addFields": {
"violations": {
"$map": {
"input": "$violations",
"in": {
"$mergeObjects": [
"$$this",
{
"inspection_date": {
"$dateFromString": {
"dateString": "$$this.inspection_date",
"format": "%Y-%m-%d",
"onError": null,
"onNull": null
}
}
}
],
},
}
}
}
},
])
Mongo Playground Sample Execution

How to extract labels from json?

I have this json:
"client":{
"datas": {
"number1": 14,
"number2": 2,
"number3": 1,
"city": [
"paris",
"tokyo",
"nyc",
"london"
]
},
}
I want to extract label/value peers for the "number" labels but not for the array like that:
number 1 : 14
number 2 : 2
numer 3 : 1
paris,
tokyo,
nyc,
london
Edit:
I tried this loop:
for (i in datas)
{ newDatas = clents.datas[i]; }
I get :
14
2
1
Array [
"paris",
"tokyo",
"nyc",
"london", ]

access value in array from Json response typesciprt

How can i access subjects in Json response and take the value which in types using typescript ?
Json Response :
{
"$id": "1",
"Council_ID": 102,
"place": "bla bla bla",
"number": "4644",
"type": 2,
"user_Id": 15,
"subjects": [
{
"$id": "2",
"subjectCode": "464",
"type": 1,
"branch": "cairo",
"gender": true
},
{
"$id": "3",
"subjectCode": "466",
"type": 5,
"branch": "alex",
"gender": true
}
],
"absence": []
}
meeting.component.ts :
this.dataStorageService.getCouncilId(this.meetingID).subscribe(response => {
this.subjectsWithID = response.json();
console.log(this.subjectsWithID, 'All Response')
this.typee = this.subjectsWithID.subjects.type;
console.log(this.typee, 'bla bla');
});
Here is how to access the values
this.subjectsWithID.Council_ID; // 102
this.subjectsWithID.type; // 2
this.subjectsWithID.$id; // '1'
this.subjectsWithID.subjects[0].$id; // '2'
this.subjectsWithID.subjects[0].type; // 1
this.subjectsWithID.subjects[1].$id; // '3'
this.subjectsWithID.subjects[1].type; // 5
I might have misunderstood your question. If so please provide some example of the desired output.
this.dataStorageService
.getCouncilId(this.meetingID)
.map(json)
.map(response => response.subjects.map(subject => subject.type))
.subscribe(response => { ... });

C3 - Timeseries chart with JSON and categories

I am using C3 library for the first time and I think it's a good alternative to D3 for designing simple and reusable charts with no pain.
However, I have some issues in designing a timeseries chart.
Here is an example of the JSON file I will use to generate my chart:
data: {
json: [
{
"city": "Paris",
"date": "2016-09-01",
"event": 234
},
{
"city": "Paris",
"date": "2016-09-02",
"event": 891
},
{
"city": "Paris",
"date": "2016-09-03",
"event": 877
},
{
"city": "Berlin",
"date": "2016-09-01",
"event": 190
},
{
"city": "Berlin",
"date": "2016-09-02",
"event": 234
},
{
"city": "Berlin",
"date": "2016-09-03",
"event": 231
},
{
"city": "London",
"date": "2016-09-01",
"event": 23
},
{
"city": "London",
"date": "2016-09-02",
"event": 12
},
{
"city": "London",
"date": "2016-09-03",
"event": 89
},
],
The problem is that I can not set both my axis x: as a timeseries type and the key "city" as a category type.
For now I have:
keys: {
x: 'period',
value: ['event'],
},
axis: {
x: {
type: 'timeseries',
tick: {
format: '%Y-%m-%d'
}
}
},
type: 'spline'
And the corresponding Plunker: http://plnkr.co/edit/T1aLWQpaFwdu2zsWCa3d
I would like to have 3 splines, corresponding to the 3 cities that are retrieved from the JSON file.
Can you help me achieve this ?
Thank you very much :)
You need to wrangle your data into a format that c3 finds acceptable, which is akin to the example here -->https://jsfiddle.net/maxklenk/k9Dbf/
For yours we'd need an array of entries like
[{
date = val
London = val
Paris = val
Berlin = val
},
...
]
To do that we need to manipulate the original json:
var json = <defined here>
// group json by date
var nestedData = d3.nest().key(function(d) { return d.date; }).entries(json);
var cities = d3.set(); // this keeps a record of the cities mentioned so we don't need to hard-code them later on
// run through the dates and make new objects of city=entry pairs (and the date=whatever)
// all stored in a new array (formattedData) which we can feed to the chart json argument
var formattedData = nestedData.map (function (entry) {
var values = entry.values;
var obj = {};
values.forEach (function (value) {
obj[value.city] = value.event;
cities.add(value.city);
})
obj.date = entry.key;
return obj;
});
var chart = c3.generate({
data: {json: formattedData,
keys: {
x: 'date', // it's possible to specify 'x' when category axis
value: cities.values(),
}
},
...
See the edited plunkr at http://plnkr.co/edit/5xa4z27HbHQbjcfpRLpQ?p=preview