I have a json file like this, I woiuld like to have a jq
based solution to transform this:
{
... fields1 ...
"any": [], "other": 1,
"aa.2": "second.", "aa.1": "first ",
... fields2 ...
"bla.bla.0": "aa", "bla.bla.2": "bb",
... fields3 ...
}
I want an output grouped by prefix before ".<number>" concatenated as string (values for those can be assumed to be strings). The order of the fields with ".<number>" ending is not necessarily ordered.
fields1,2,3 are other fields need to be kept (the order of the fields in the output object do not have to be kept)
The fields with ".<number>" ending must be removed in the output, only the concatenated final value is to be kept.
{
... fields1 ...
"any": [], "other": 1,
"aa": "first second.",
... fields2 ...
"bla.bla": "aabb",
... fields3 ...
}
I tried to find a solution using the manual but I did not succeed.
2
Answers
You’re looking for something like this:
You can slice by the position of the last dot
rindex(".")
to find the future key and the number parts, thenreduce
by the groups to iteratively construct the result object. The values can be constructed by sorting the number part convertedtonumber
.Demo