'How to compare two JSON string in flutter?
I have two long JSON string and what I need is compare these strings and find if they are equals or not, if it's not equal I can get list1 which contains (JSON1-JSON2) and list2 contains (JSON2-JSON1)
I find this ( https://github.com/google/dart-json_diff ) but it's very annoying and I don't know how to use this
I mean something like this
final json1 = await http.get('https://jsonplaceholder.typicode.com/posts');
final json2 = await http.get('https://jsonplaceholder.typicode.com/posts');
var list1= compare(json1,json2);
var list2= compare(json2,json1);
List<dynamic> compare (List<dynamic> json1,List<dynamic> json1){
.
.
code?
.
.
return results;
}
/////////////////
json1=[
{
"userId": 1,
"id": 1,
"title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
"body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
},
{
"userId": 1,
"id": 2,
"title": "qui est esse",
"body": "est rerum tempore vitae\nsequi sint nihil reprehenderit dolor beatae ea dolores neque\nfugiat blanditiis voluptate porro vel nihil molestiae ut reiciendis\nqui aperiam non debitis possimus qui neque nisi nulla"
},
{
"userId": 1,
"id": 3,
"title": "ea molestias quasi exercitationem repellat qui ipsa sit aut",
"body": "et iusto sed quo iure\nvoluptatem occaecati omnis eligendi aut ad\nvoluptatem doloribus vel accusantium quis pariatur\nmolestiae porro eius odio et labore et velit aut"
},
]
/////////////////
json2=[
{
"userId": 1,
"id": 1,
"title": "sunt aut facere repellat ",
"body": "quia et suscipit\nsuscipit recusandae"
},
{
"userId": 1,
"id": 2,
"title": "qui est esse",
"body": "est rerum tempore vitae\nsequi "
},
{
"userId": 1,
"id": 3,
"title": "ea molestias quasi",
"body": "et iusto sed quo iure\nvoluptatem occaecati"
},
{
"userId": 1,
"id": 4,
"title": "qui est esse",
"body": "est rerum tempore vitae\nsequi "
},
]
//////////
list1=[
{
"userId": 1,
"id": 4,
"title": "qui est esse",
"body": "est rerum tempore vitae\nsequi "
},
]
thank u
Solution 1:[1]
Ok ? this code working for me:
Future<String> _loadTestDataAssetsCloud() async {
return await rootBundle.loadString('assets/testDataCloud.json');
}
Future<String> _loadTestDataAssetsUrl() async {
return await rootBundle.loadString('assets/testDataUrl.json');
}
Future testData() async {
var jsonUrl = await _loadTestDataAssetsUrl();
Map decodedUrl = jsonDecode(jsonUrl);
List<dynamic> individualsUrl = (((decodedUrl['CONSOLIDATED_LIST']
as Map)['INDIVIDUALS'] as Map)['INDIVIDUAL']);
var jsonCloud = await _loadTestDataAssetsCloud();
Map decodedCloud = jsonDecode(jsonCloud);
List<dynamic> individualsCloud = (((decodedCloud['CONSOLIDATED_LIST']
as Map)['INDIVIDUALS'] as Map)['INDIVIDUAL']);
List<dynamic> newList = [];
List<dynamic> delList = [];
individualsUrl.forEach((itemUrl) {
bool a = true;
individualsCloud.forEach((itemCloud) {
if (itemUrl['REFERENCE_NUMBER'] == itemCloud['REFERENCE_NUMBER']) {
a = false;
}
});
if (a == true) {
newList.add(itemUrl);
}
});
print('newList');
// print(newList);
print(newList.length);
newList.forEach((f){
print(f['REFERENCE_NUMBER']);
});
individualsCloud.forEach((itemCloud) {
bool d = true;
individualsUrl.forEach((itemUrl) {
if (itemCloud['REFERENCE_NUMBER'] == itemUrl['REFERENCE_NUMBER']) {
d = false;
}
});
if (d == true) {
delList.add(itemCloud);
}
});
print('delList');
// print(delList);
print(delList.length);
delList.forEach((f){
print(f['REFERENCE_NUMBER']);
});
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | ALI HUSSEIN |
