Parse data to two separate lists with condition in Flutter - json

[
{
"name": "ITEM-1",
"desc": "DESCRIPTION",
"listNo" :"0"
},
{
"name": "ITEM-2",
"desc": "DESCRIPTION",
"listNo" :"0"
},
{
"name": "ITEM-3",
"desc": "DESCRIPTION",
"listNo" :"0"
},
{
"name": "ITEM-4",
"desc": "DESCRIPTION",
"listNo" :"1"
},
{
"name": "ITEM-5",
"desc": "DESCRIPTION",
"listNo" :"1"
},
{
"name": "ITEM-6",
"desc": "DESCRIPTION",
"listNo" :"1"
}
]
How to implement this model in futter? I want to show the two lists as shown in and data from JSON above.
The condition for LIST-A is "listNo"="0" and for LIST-B is "listNo"="1".

There are multiple ways to do it but I think the most easiest to understand solution would be to do the following:
import 'dart:convert';
void main() {
const input = '''
[
{
"name": "ITEM-1",
"desc": "DESCRIPTION",
"listNo" :"0"
},
{
"name": "ITEM-2",
"desc": "DESCRIPTION",
"listNo" :"0"
},
{
"name": "ITEM-3",
"desc": "DESCRIPTION",
"listNo" :"0"
},
{
"name": "ITEM-4",
"desc": "DESCRIPTION",
"listNo" :"1"
},
{
"name": "ITEM-5",
"desc": "DESCRIPTION",
"listNo" :"1"
},
{
"name": "ITEM-6",
"desc": "DESCRIPTION",
"listNo" :"1"
}
]
''';
final list1 = <Map<String, dynamic>>[];
final list2 = <Map<String, dynamic>>[];
for (final map in json.decode(input)) {
if (map['listNo'] == '0') {
list1.add(map as Map<String, dynamic>);
} else {
list2.add(map as Map<String, dynamic>);
}
}
print(list1);
// [{name: ITEM-1, desc: DESCRIPTION, listNo: 0}, {name: ITEM-2, desc: DESCRIPTION, listNo: 0}, {name: ITEM-3, desc: DESCRIPTION, listNo: 0}]
print(list2);
// [{name: ITEM-4, desc: DESCRIPTION, listNo: 1}, {name: ITEM-5, desc: DESCRIPTION, listNo: 1}, {name: ITEM-6, desc: DESCRIPTION, listNo: 1}]
}
Alternative you could also do the following if you think that looks more clear:
final parsedJson = json.decode(input) as List<dynamic>;
final list1 = [
...parsedJson
.cast<Map<String, dynamic>>()
.where((map) => map['listNo'] == '0')
];
final list2 = [
...parsedJson
.cast<Map<String, dynamic>>()
.where((map) => map['listNo'] == '1')
];

Related

How to convert Json to Map in Dart/Flutter?

I have json data but i need to convert the json to Map in dart/flutter.
The json data is :
{"kabupaten": [
{
"jec_kabupaten_id": "71",
"jec_propinsi_id": "8",
"name": "Aceh Barat"
},
{
"jec_kabupaten_id": "76",
"jec_propinsi_id": "8",
"name": "Aceh Barat Daya"
},
{
"jec_kabupaten_id": "91",
"jec_propinsi_id": "9",
"name": "Medan"
},
{
"jec_kabupaten_id": "92",
"jec_propinsi_id": "9",
"name": "Sinabung"
}
]}
I want to convert to this Map with dart/flutter:
{
"8":{
"71":"Aceh Barat",
"76":"Aceh Barat Daya",
"68":"Aceh Besar"
},
"9":{
"91":"Medan",
"92":"Sinabung"
}
}
It is possible to transform the data using fold for example.
void main() {
const data = {
"kabupaten": [
{
"jec_kabupaten_id": "71",
"jec_propinsi_id": "8",
"name": "Aceh Barat",
},
{
"jec_kabupaten_id": "76",
"jec_propinsi_id": "8",
"name": "Aceh Barat Daya",
},
{
"jec_kabupaten_id": "91",
"jec_propinsi_id": "9",
"name": "Medan",
},
{
"jec_kabupaten_id": "92",
"jec_propinsi_id": "9",
"name": "Sinabung",
},
],
};
final result = data['kabupaten']?.fold<Map<String, Map<String, String>>>(
{},
(prev, elem) => prev
..[elem['jec_propinsi_id']!] ??= {}
..[elem['jec_propinsi_id']!]![elem['jec_kabupaten_id']!] = elem['name']!,
);
print(result);
}
An alternative would be to build up the result in a for loop:
final result = <String, Map<String, String>>{};
for (final elem in data['kabupaten'] ?? []) {
result[elem['jec_propinsi_id']!] ??= {};
result[elem['jec_propinsi_id']!]![elem['jec_kabupaten_id']!] = elem['name']!;
}

TypeScript convert json structure

I cant figure out how to make this conversion iterating a json.
I have this pojo in my backend:
class Part{
Long id;
String name;
Set<Part> parts = new HashSet<>();
}
Every part can have parts and this part more parts and so on.
I get this parts from httpclient in angular and get this json:
[{
"id": 1,
"name": "Parts A and B",
"parts": [{
"id": 2,
"name": "A",
"parts": [{
"id": 4,
"name": "A1",
"parts": []
}]
},
{
"id": 3,
"name": "B",
"parts": []
}
]
},
{
"id": 2,
"name": "A",
"parts": []
},
{
"id": 3,
"name": "B",
"parts": []
},
{
"id": 4,
"name": "A1",
"parts": []
}
]
And need to convert to this to populate a PrimeNG TreeTable:
{
"data": [{
"data": {
"name": "Parts A and B",
"id": "1"
},
"children": [{
"data": {
"name": "Part A",
"id": "2"
},
"children": [{
"data": {
"name": "A1",
"id": "4"
}
}]
},
{
"data": {
"name": "Part B",
"id": "3"
},
"children": []
}
]
},
{
"data": {
"name": "Part A",
"id": "2"
},
"children": []
},
{
"data": {
"name": "Part B",
"id": "3"
},
"children": []
},
{
"data": {
"name": "A1",
"id": "4"
},
"children": []
}
]
}
How can I do that?
In angular I get this in an array parts: Part[] and need partsTree: TreeNode[]
Thanks!!!
Its just a simple conversion by a map;
interface PartAPI{
id: number;
name: string;
parts : PartAPI[];
}
interface Data {
id: number;
name: string;
}
interface Part {
data : Data;
children : Part[];
}
console.log('a')
let convert = (inputArr: PartAPI[] = []) : Part[] => {
return inputArr.map(partApi => ({ data : { id : partApi.id , name : partApi.name }, children: convert(partApi.parts) }) as Part)
}
let data : PartAPI[] = [{
"id": 1,
"name": "Parts A and B",
"parts": [{
"id": 2,
"name": "A",
"parts": [{
"id": 4,
"name": "A1",
"parts": []
}]
},
{
"id": 3,
"name": "B",
"parts": []
}
]
},
{
"id": 2,
"name": "A",
"parts": []
},
{
"id": 3,
"name": "B",
"parts": []
},
{
"id": 4,
"name": "A1",
"parts": []
}
]
console.log(convert(data));

How to modify json using JRS223 preprocessor in jmeter

Json body looks like this:
{
"access_key": "",
"erid": "",
"ch_sms": {
"messages": [
{
"urlsh": false,
"sp": "123",
"vp": 20,
"heid": 1,
"teid": ,
"peid": ,
"tmid": 4,
"msg": {
"txt": "hello"
},
"da": [
{
"number": "97278",
"cc": "IN",
"uid": "uid1",
"tags": [
"key",
"Value"
]
}
]
}
],
"metadata": {
"chver": "1.0",
"cburl": "",
"heid": 1,
"teid": ,
"peid": ,
"tmid": 4,
"Oa": "",
"flash": false,
"tags": [
"key",
"tag1"
]
}
}
}
We need to add multiple "da" sections based on user input. For example if I give input from a CSV file as 3 then "da" section will repeat 3 times and it will look like as follow:
{
"access_key": "",
"erid": "",
"ch_sms": {
"messages": [
{
"urlsh": false,
"sp": "123",
"vp": 20,
"heid": 1,
"teid": ,
"peid": ,
"tmid": 4,
"msg": {
"txt": "hello"
},
"da": [
{
"number": "97278",
"cc": "IN",
"uid": "uid1",
"tags": [
"key",
"Value"
]
},
{
"number": "97278",
"cc": "IN",
"uid": "uid1",
"tags": [
"key",
"Value"
]
},
{
"number": "97278",
"cc": "IN",
"uid": "uid1",
"tags": [
"key",
"Value"
]
}
]
}
],
"metadata": {
"chver": "1.0",
"cburl": "",
"heid": 1,
"teid": ,
"peid": ,
"tmid": 4,
"Oa": "",
"flash": false,
"tags": [
"key",
"tag1"
]
}
}
}
You can try this,
import org.json.simple.JSONObject
JSONObject data = new JSONObject()
data.put("number", "97278")
data.put("cc", "IN")
data.put("uid", "uid1")
data.put("tags", ["key","Value"])
int da = Integer.parseInt(vars.get("da"))
ArrayList<Object> listData = new ArrayList<Object>()
for(int i=0; i < da; i++)
{
listData.add(data.toString())
}
log.info("da: " + listData )
Result log:
da: [{"cc":"IN","number":"97278","uid":"uid1","tags":["key","Value"]}, {"cc":"IN","number":"97278","uid":"uid1","tags":["key","Value"]}, {"cc":"IN","number":"97278","uid":"uid1","tags":["key","Value"]}]

How to get the required json path?

I had this type of JSON structure and I want to fetch the upper element in the hierarchy on applying condition on lower element in hierarchy
{
"name": "ninja",
"contry": "India",
"Account": [
{
"id": "123",
"orgId": 223,
"investment": [
{
"invetmentId": "111",
"name": "India tech",
"performance": [
{
"id": "123",
"performanceSet": [
{
"amount": "210",
"currency": "YEN"
},
{
"amount": "231",
"currency": "USD"
},
{
"amount": "233",
"currency": "USD"
},
{
"amount": "250",
"currency": "IND"
}
],
"loyal": "250"
}
]
},
{
"invetmentId": "111",
"name": "USA tech",
"performance": [
{
"id": "245",
"performanceSet": [
{
"amount": "956",
"currency": "ASD"
},
{
"amount": "231",
"currency": "USD"
},
{
"amount": "233",
"currency": "USD"
},
{
"amount": "250",
"currency": "IND"
}
],
"loyal": "250"
}
]
}
]
}
]
}
So what I need is I need the investment name for which performance.id = 123 i.e it should return "India tech".So I want to know if that can be possible or not if yes how can I get that?
This method doesn't use nested forEach which is O(n²) and it stops iterating once it finds the matched result
data = { name: "ninja", contry: "India", Account: [ { id: "123", orgId: 223, investment: [ { invetmentId: "111", name: "India tech", performance: [ { id: "123", performanceSet: [ { amount: "210", currency: "YEN", }, { amount: "231", currency: "USD", }, { amount: "233", currency: "USD", }, { amount: "250", currency: "IND", }, ], loyal: "250", }, ], }, { invetmentId: "111", name: "USA tech", performance: [ { id: "245", performanceSet: [ { amount: "956", currency: "ASD", }, { amount: "231", currency: "USD", }, { amount: "233", currency: "USD", }, { amount: "250", currency: "IND", }, ], loyal: "250", }, ], }, ], }, ], };
res = [];
finfperf = (id) => {
data.Account.forEach((inv) =>
res.push(
inv.investment.find((o) => o.performance.some((a) => a.id == id)).name
)
);
return res;
};
console.log(finfperf(123));
console.log(finfperf(245));

Re-arrange JSON file (using adjacency matrix)

I have a json file that looks like this:
[
{
"id": 1,
"country": "Greece",
"names": [
"Alex",
"Betty",
"Catherine",
"Dave",
"Edward",
"Frank",
"George",
"Helen",
"Irene"
]
},
{
"id": 2,
"country": "US",
"names": [
"John",
"Alex",
"Edward",
"Kate",
"Robert",
"Irene",
"Tim",
"Sam"
]
},
{
"id": 3,
"country": "France",
"names": [
"Helen",
"Kate",
"Louise",
"Tim",
"Catherine",
"Arthur",
"Frank",
"Natalie",
"Dave"
]
},
{
"id": 4,
"country": "India",
"names": [
"Ritesh",
"Alex",
"Betty",
"Robert"
]
},
{
"id": 5,
"country": "India",
"names": [
"Nafeez",
"Tom",
"Natalie",
"Gunar",
"Louise",
"Arthur"
]
}
]
I want it to be "name centered" and look like this:
{
"groups": [
{
"gr_id":1
"name":"Alex",
"country":"Greece"
},
.........
{
"gr_id":1
"name":"Irene",
"country":"Greece"
},
{
"gr_id":2
"name":"John",
"country":"US"
..........
{
"gr_id":2
"name":"Sam",
"country":"US"
},
{
"gr_id":3
"name":"Helen",
"country":"France"
},
.........
{
"gr_id":3
"name":"Dave",
"country":"France"
},
{
"gr_id":4
"name":"Ritesh",
"country":"India"
},
........
{
"gr_id":4
"name":"Robert",
"country":"India"
},
{
"gr_id":5
"name":"Nafeez",
"country":"India"
},
...........
{
"gr_id":5
"name":"Arthur",
"country":"India"
}
],
"links": [
{
"source":"Alex"
"target":"Irene",
"count":1
"country":"Greece"
},
...
{
"source":"Alex"
"target":"Arthur",
"count":0
"country":"India"
},
...
]
}
For count in Links I have an adjacency matrix for each country/name (csv format) like this :screenshot of csv file (ad. matrix for India)
This json is just an example. I have much bigger file (I need it for D3 graph visualization)
Reduce() and map() work perfectly for this. This basically takes each item and then maps over the names, appending the results of map() to an array:
let obj = {}
obj.groups = json.reduce(
(acc, curr) => acc.concat(curr.names.map(
item => ({gr_id: curr.id, country: curr.country, name: item})
)), [])
console.log(obj)
// { groups:
// [ { gr_id: 1, country: 'Greece', name: 'Alex' },
// { gr_id: 1, country: 'Greece', name: 'Betty' },
// ...etc
// ]
// }