Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request - Sorting JSON Objects by Attribute #689

Closed
1 of 4 tasks
tiymat opened this issue Aug 17, 2023 · 5 comments
Closed
1 of 4 tasks

Feature Request - Sorting JSON Objects by Attribute #689

tiymat opened this issue Aug 17, 2023 · 5 comments
Labels
deployed to `develop` Feature built. Currently BETA testing in the `develop` branch enhancement New features

Comments

@tiymat
Copy link
Contributor

tiymat commented Aug 17, 2023

Describe the problem:
It would be really cool to be able to sort an array of JSON objects based on a given attribute.

Possible ways to implement:
Possibly msort could have a flag indicating which attribute to sort on? Or, even better, the flag could accept multiple arguments (comma-separated?) So first the objects are sorted on the first attribute, and then all of the objects with the same value for that attribute are sorted on the second attribute, etc.

Additional context:
msort's current behavior seems to be to sort using the attributes in alphabetical order, unless I'm misunderstanding it.

misc » cat employees.json
[{"Name":"Jake","Department":"Sales"},{"Name":"Carl","Department":"Accounting"},{"Name":"Abigail","Department":"IT"}]
misc » cat employees.json -> :json: msort -> foreach { -> cat }
{"Department":"Accounting","Name":"Carl"}
{"Department":"IT","Name":"Abigail"}
{"Department":"Sales","Name":"Jake"}

My request is to be able to sort on a specific attribute like Name, e.g.

cat employees.json -> :json: msort --attribute Name -> foreach { -> cat }
{"Department":"IT","Name":"Abigail"}
{"Department":"Accounting","Name":"Carl"}
{"Department":"Sales","Name":"Jake"}

Documentation:
Please rate your success with referring to the docs @ https://murex.rocks

  • I haven't read the docs / the solution should be more discoverable
  • I have read the docs but the content wasn't clear
  • I have read the docs but this query was missing
  • I have read the docs and this query was answered but I'm still having problems

Maybe there's already a way of doing it, but if so I wasn't able to find it in the documentation.

@tiymat tiymat added the enhancement New features label Aug 17, 2023
@tiymat
Copy link
Contributor Author

tiymat commented Aug 17, 2023

Right after I submitted that issue I had an idea for a workaround doing basically this

misc » cat employees.json -> :json: foreach --jmap employee { $employee -> ["Name"] } { $employee }
{
    "Abigail": "{\"Department\":\"IT\",\"Name\":\"Abigail\"}",
    "Carl": "{\"Department\":\"Accounting\",\"Name\":\"Carl\"}",
    "Jake": "{\"Department\":\"Sales\",\"Name\":\"Jake\"}"
}

But
A) It seems really hacky,
B) It only works when the values of the attribute you want to sort on are unique, and
C) You'd still have to iterate over the keys of the resulting map object, which I can't remember if there's a way of doing -- I'll have to check later.

@lmorg
Copy link
Owner

lmorg commented Aug 17, 2023

Good suggestion. The problem here would that JSON maps are unordered (as per the JSON specification) so the output cannot be json type otherwise the next tool you use will just scramble the ordering again.

That said, I don’t think your request is an unreasonable one. It might just need a little bit of magic to get working right.

lmorg added a commit that referenced this issue Aug 26, 2023
@lmorg
Copy link
Owner

lmorg commented Aug 26, 2023

@tiymat I think the best approach here, since we are talking about relational data, is probably to convert the object to a table. eg

» open ~/employees.json
[{"Name":"Jake","Department":"Sales"},{"Name":"Carl","Department":"Accounting"},{"Name":"Abigail","Department":"IT"}]

reformat as a CSV:

» open ~/employees.json -> format csv
Department,Name
Sales,Jake
Accounting,Carl
IT,Abigail

run SQL against table:

» open ~/employees.json -> format csv -> select Name, Department order by Name
Name,Department
Abigail,IT
Carl,Accounting
Jake,Sales

a formatting from a map to a table fix is in the commit above (7d86cc2) and, currently, you cannot convert from a table back to a map

@tiymat
Copy link
Contributor Author

tiymat commented Aug 26, 2023

Makes sense, haven't tested it yet but I think that would work for the use case I envisioned that caused me to make this feature request.

I actually had no idea about the select builtin, that'll come in handy!

@lmorg lmorg added the in progress Issue is currently being worked on (possibly in a feature branch) label Aug 26, 2023
@lmorg lmorg mentioned this issue Aug 27, 2023
@lmorg lmorg added the deployed to `develop` Feature built. Currently BETA testing in the `develop` branch label Sep 5, 2023
@lmorg lmorg closed this as completed Nov 28, 2023
@lmorg
Copy link
Owner

lmorg commented Nov 28, 2023

I believe this should have been closed a while ago but I missed it?

@lmorg lmorg removed the in progress Issue is currently being worked on (possibly in a feature branch) label Jul 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
deployed to `develop` Feature built. Currently BETA testing in the `develop` branch enhancement New features
Projects
None yet
Development

No branches or pull requests

2 participants