0

i am unable to remove duplicates in Json. In below Json if title field has same values, i need to remove the duplicate node. IN below json title named "car" was repeated 3 times and i need to remove duplicates based on title field. I followed below stackoverflow links but not solved my problem.

Remove duplicate value in dart

Actual Json:

{
    "totalSize": 6,
    "done": true,
    "records": [{
            "attributes": {
                "type": "ContentVersion",
                "url": "https://sampleUrl"
            },
            "Id": "123456",
            "Title": "car",
            "Team_Category__c": "Vehicle"
        },
        {
            "attributes": {
                "type": "ContentVersion",
                "url": "https://sampleUrl"
            },
            "Id": "123456",
            "Title": "car",
            "Team_Category__c": "Vehicle"
        },

        {
            "attributes": {
                "type": "ContentVersion",
                "url": "https://sampleUrl"
            },
            "Id": "123456",
            "Title": "cycle",
            "Team_Category__c": "Vehicle"
        },
        {
            "attributes": {
                "type": "ContentVersion",
                "url": "https://sampleUrl"
            },
            "Id": "123456",
            "Title": "aeroplane",
            "Team_Category__c": "Vehicle"
        },
        {
            "attributes": {
                "type": "ContentVersion",
                "url": "https://sampleUrl"
            },
            "Id": "123456",
            "Title": "car",
            "Team_Category__c": "Vehicle"
        }
    ]
}
4
  • can you please describe the relation to flutter? If you get this json as a response from your database you would just read out the data you need and don't need to remove anything... Commented Aug 14, 2019 at 20:11
  • In my database, i have problem some title fields with same name are repeating. When parsing the json from mobile application ios flutter, i want to manipulate the the Json to remove duplicates using set feature in dart. Below link is also similar to my problem, but not solved for me. stackoverflow.com/questions/54087857/… Commented Aug 14, 2019 at 20:19
  • the duplicates would be removed if you convert it to a map Commented Aug 14, 2019 at 20:26
  • Can you please share sample code like below var newUniqueResponse = jsonResponseArray.map((records) => [records.title]); Commented Aug 14, 2019 at 22:11

2 Answers 2

2

Add kt_dart: to your pubspec.

Then import it in your dart file import 'package:kt_dart/kt.dart';

Then:

var json = jsonDecode(yourJsonAsString);
var records = mutableListFrom(json["records"]);
var distinct = records.distinctBy((it) => it["Title"]);
Sign up to request clarification or add additional context in comments.

Comments

0

if nothing works out, you can create new similar structure and add only distinct items into it as below. I am not a dart programmer but it is a naive solution.

import 'dart:convert';

void main() {

  var jsonData = '[{ "name" : "Dane", "alias" : "FilledStacks" },{ "name" : "Dane", "alias" : "FilledStacks" }]';

  List uniqueList = new List();
  List parsedJson = json.decode(jsonData);
  
  for (var jsonElement in parsedJson) {

    bool isPresent = false;
    for (var uniqueEle in uniqueList) {
      if (uniqueEle['name'] == jsonElement['name']) isPresent = true;
    }
    if (!isPresent) uniqueList.add(jsonElement);
  }
  
print('$uniqueList');

  print('');

  print('${parsedJson.runtimeType} : $parsedJson');

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.