digitalmars.D.learn - Removind duplicates for JSON string
- Vino (53/53) Nov 01 2020 Hi All,
- Anonymouse (11/30) Nov 01 2020 I can only test with Phobos (no asdf on run.dlang.io), but isn't
- Imperatorn (3/15) Nov 01 2020 Tip:
Hi All, Request your help on how to remove duplicates in JSON. Code: import asdf; import std.algorithm : map, filter, uniq; import std.container.array; import std.stdio : writeln; import std.typecons : Tuple, tuple; import std.array; void main() { string apidata = `{ "items": [ { "name":"DEV", "Configuration":{"type":"D1"} }, { "name":"DEV" }, { "name":"DEV", "Configuration":{"type":"D1"} }, { "name":"QAS", "Configuration":{"type":"Q1"} }, { "name":"QAS", "Configuration":{"type":"Q1"} }, { "name":"QAS" }, { "name":"PRD", "Configuration":{"type":"P1"} }, { "name":"PRD", "Configuration":{"type":"P1"} }, { "name":"PRD" } ] }`; Array!(Tuple!(string, string)) data = parseJson(apidata)["items"] .byElement .map!(item => tuple( item["name"].get!string("default"), item["Configuration","type"].get!string("default") )); writeln(data[]); } Output: The above code produces the below output [ Tuple!(string, string)("DEV", "D1"), Tuple!(string, string)("DEV", "default"), Tuple!(string, string)("DEV", "D1"), Tuple!(string, string)("QAS", "Q1"), Tuple!(string, string)("QAS", "Q1"), Tuple!(string, string)("QAS", "default"), Tuple!(string, string)("PRD", "P1"), Tuple!(string, string)("PRD", "P1"), Tuple!(string, string)("PRD", "default") ] Required Output [ Tuple!(string, string)("DEV", "D1"), Tuple!(string, string)("QAS", "Q1"), Tuple!(string, string)("PRD", "P1"), ] From, Vino.B
Nov 01 2020
On Sunday, 1 November 2020 at 09:14:35 UTC, Vino wrote:[ Tuple!(string, string)("DEV", "D1"), Tuple!(string, string)("DEV", "default"), Tuple!(string, string)("DEV", "D1"), Tuple!(string, string)("QAS", "Q1"), Tuple!(string, string)("QAS", "Q1"), Tuple!(string, string)("QAS", "default"), Tuple!(string, string)("PRD", "P1"), Tuple!(string, string)("PRD", "P1"), Tuple!(string, string)("PRD", "default") ] Required Output [ Tuple!(string, string)("DEV", "D1"), Tuple!(string, string)("QAS", "Q1"), Tuple!(string, string)("PRD", "P1"), ] From, Vino.BI can only test with Phobos (no asdf on run.dlang.io), but isn't just normal sort and uniq what you want? data[] .array .sort .uniq!((tup1, tup2) => tup1[0] == tup2[0]); https://run.dlang.io/is/a3KHaE Sorting "default" values can be tricky but they conveniently cede to explicit D1/Q1/P1 values because of ASCII. Otherwise you would have to provide different sort and uniq predicates.
Nov 01 2020
On Sunday, 1 November 2020 at 13:31:19 UTC, Anonymouse wrote:On Sunday, 1 November 2020 at 09:14:35 UTC, Vino wrote:Tip: You can find asdf under "Add library" on run.dlang.io[...]I can only test with Phobos (no asdf on run.dlang.io), but isn't just normal sort and uniq what you want? data[] .array .sort .uniq!((tup1, tup2) => tup1[0] == tup2[0]); https://run.dlang.io/is/a3KHaE Sorting "default" values can be tricky but they conveniently cede to explicit D1/Q1/P1 values because of ASCII. Otherwise you would have to provide different sort and uniq predicates.
Nov 01 2020