Thread overview | |||||
---|---|---|---|---|---|
|
November 01, 2020 Removind duplicates for JSON string | ||||
---|---|---|---|---|
| ||||
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 |
November 01, 2020 Re: Removind duplicates for JSON string | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vino | 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.B 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. |
November 01, 2020 Re: Removind duplicates for JSON string | ||||
---|---|---|---|---|
| ||||
Posted in reply to Anonymouse | On Sunday, 1 November 2020 at 13:31:19 UTC, Anonymouse wrote:
> On Sunday, 1 November 2020 at 09:14:35 UTC, Vino wrote:
>> [...]
>
> 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.
Tip:
You can find asdf under "Add library" on run.dlang.io
|
Copyright © 1999-2021 by the D Language Foundation