3
1
I have a JSON output in the following format:
{
"DaysCfg": {
"Range": {
"lowerDate": "2017-07-28T00:00:00.000-04:00",
"upperDate": "2017-08-04T00:00:00.000-04:00"
},
"DaysInPeriod": 8,
"DaysToSchedule": [
0,
1,
2,
3,
4,
5,
6
]
},
"DepartmentsID": [
138837,
139734,
141934,
142436,
149687,
151049
],
"EmployeesID": [
5039,
5170,
5889,
6051,
6236,
7208,
7281,
8776,
8781,
8936,
9261
],
"EndDate": "2017-08-03T23:59:00.000-04:00",
"IntervalSize": 15,
"IsActivitiesEnabled": true,
"ModifyExisting": false,
"OrignId": 134721,
"PrimaryOption": 0,
"SchoolDays": [],
"ScChanges": [],
"StartDate": "2017-07-28T00:00:00.000-04:00",
"ZonesToSchedule": [
5,
4,
6,
3,
3,
3,
2,
14
]
}
Since I can't change the program that output it, I have to use sed
(or awk
) to compact JSON arrays myself. The desirable output would be:
{
"DaysCfg": {
"Range": {
"lowerDate": "2017-07-28T00:00:00.000-04:00",
"upperDate": "2017-08-04T00:00:00.000-04:00"
},
"DaysInPeriod": 8,
"DaysToSchedule": [0, 1, 2, 3, 4, 5, 6]
},
"DepartmentsID": [138837, 139734, 141934, 142436, 149687, 151049],
"EmployeesID": [5039, 5170, 5889, 6051, 6236, 7208, 7281, 8776, 8781, 8936, 9261],
"EndDate": "2017-08-03T23:59:00.000-04:00",
"IntervalSize": 15,
"IsActivitiesEnabled": true,
"ModifyExisting": false,
"OrignId": 134721,
"PrimaryOption": 0,
"SchoolDays": [],
"ScChanges": [],
"StartDate": "2017-07-28T00:00:00.000-04:00",
"ZonesToSchedule": [5, 4, 6, 3, 3, 3, 2, 14]
}
I have tried to come up a sed
script myself, but it is only half-cooked and not fully working:
sed -r -e :a -e '/^ *[]}],*$/!N; /": \[/s/\n +//; ta' -e 'P;D'
Please Help. Thx.
1
I think
– uzsolt – 2017-07-28T19:46:24.373sed
isn't the best tool for this problem. You should try some JSON-parser/formatter, for examplejq
(https://stedolan.github.io/jq/). Maybe you want to check https://stackoverflow.com/questions/9105031/how-to-beautify-json-in-python-or-through-command-line and https://stackoverflow.com/questions/352098/how-can-i-pretty-print-json-in-unix-shell-scriptFYI, the output that I wanted to work on, is created exactly by
jq
, aC
program, thus I'm not interested in any Python solutions. @uzsolt. What you see here is only a selected representation of of 4~6M output that I'm having. If you don't understand the abovesed
command, then it is inappropriate for you to make the judgment whethersed
is the best tool or not. – xpt – 2017-07-28T20:14:11.807So, what do you want exactly? You want delete '\n' (after a comma) if we are inside brackets (
[
and]
). If you want do it withsed
just do it! It isn't impossible but "FYI" there are better tools. I'm curious about thesed
-way solution. Go for it! (Someone downvoted your question - FYI not me) – uzsolt – 2017-07-29T12:34:34.677Thanks for the input @uzsolt. OK, I gotya. I'll forget about
sed
and do it inawk
then. – xpt – 2017-07-29T13:44:41.520The down-voting shows nothing but there are narrow minded and mean people out in the wild. I'll do it in
awk
and post back. – xpt – 2017-07-29T13:48:03.823I think
awk
is better in this case :) – uzsolt – 2017-07-29T19:54:34.970