Convert text file to JSON Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) 2019 Community Moderator Election Results Why I closed the “Why is Kali so hard” questionjq parse json log fileConvert /proc/cpuinfo to a JSON in linux bashConvert sqlite output to jsonGenerate .txt file with specific content from an invalid 3GB .json fileHow to convert a JSON file into a different formatConvert JSON array of strings to file paths in BashHow to convert embedded (quoted) json string to jsonConvert JSON to CSVhow to convert text file to json string (replace newlines with “n”)Extract .json from a text file with arbitrary text
Quick way to create a symlink?
What would be the ideal power source for a cybernetic eye?
Dating a Former Employee
Is it cost-effective to upgrade an old-ish Giant Escape R3 commuter bike with entry-level branded parts (wheels, drivetrain)?
Is it common practice to audition new musicians 1-2-1 before rehearsing with the entire band?
What is this building called? (It was built in 2002)
Do wooden building fires get hotter than 600°C?
Withdrew £2800, but only £2000 shows as withdrawn on online banking; what are my obligations?
Impact on credit score of opening and closing accounts
Fantasy story; one type of magic grows in power with use, but the more powerful they are, they more they are drawn to travel to their source
Can you shove before Attacking with Shield Master using a Readied action?
How do I make this wiring inside cabinet safer? (Pic)
Can an alien society believe that their star system is the universe?
How could we fake a moon landing now?
Generate an RGB colour grid
Is "Reachable Object" really an NP-complete problem?
8 Prisoners wearing hats
Do I really need recursive chmod to restrict access to a folder?
How to react to hostile behavior from a senior developer?
Around usage results
Ports Showing Closed/Filtered in Nmap Scans
Why are both D and D# fitting into my E minor key?
Fundamental Solution of the Pell Equation
Why didn't Eitri join the fight?
Convert text file to JSON
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
2019 Community Moderator Election Results
Why I closed the “Why is Kali so hard” questionjq parse json log fileConvert /proc/cpuinfo to a JSON in linux bashConvert sqlite output to jsonGenerate .txt file with specific content from an invalid 3GB .json fileHow to convert a JSON file into a different formatConvert JSON array of strings to file paths in BashHow to convert embedded (quoted) json string to jsonConvert JSON to CSVhow to convert text file to json string (replace newlines with “n”)Extract .json from a text file with arbitrary text
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
Using curl, I get some statistical information from my TP-Link router. I put that information to a text file. However, I need this info in JSON format so that I can use it elsewhere. The format of the text file is like this:
[0,0,0,0,0,0]0
enable=1
interval=10
action=0
[1,0,0,0,0,0]1
ipAddress=3232235848
macAddress=EC:0E:C4:4F:XX:XX
totalPkts=201
totalBytes=22914
currPkts=0
currBytes=0
currIcmp=0
currUdp=0
currSyn=0
currIcmpMax=0
currUdpMax=15
currSynMax=0
[2,0,0,0,0,0]1
ipAddress=3232235829
macAddress=00:16:E8:22:XX:XX
totalPkts=972
totalBytes=98730
currPkts=0
currBytes=0
currIcmp=0
currUdp=0
currSyn=0
currIcmpMax=0
currUdpMax=14
currSynMax=4
[3,0,0,0,0,0]1
ipAddress=3232235842
macAddress=70:14:A6:8B:XX:XX
totalPkts=154566
totalBytes=15116490
currPkts=0
currBytes=0
currIcmp=0
currUdp=0
currSyn=0
currIcmpMax=9
currUdpMax=58
currSynMax=60
I need a JSON something like:
"ipAddress" : "3232235848",
"macAddress" : "EC:0E:C4:4F:XX:XX",
"totalPkts" : "201",
"totalBytes" : "22914",
"currPkts" : "0",
"currBytes" : "0",
"currIcmp" : "0",
"currUdp" : "0",
"currSyn" : "0",
"currIcmpMax" : "0",
"currUdpMax" : "15",
"currSynMax" : "0"
"ipAddress" : "3232235829", ...
Could you show me the most efficient way of doing this? I prefer a bash script if it is possible? Tried putting "n/g'
The output:
"enable" : "1",
"interval" : "10",
"action" : "0"
"ipAddress" : "3232235848",
"macAddress" : "EC",:0E:C4:4F:XX:XX
"totalPkts" : "201",
"totalBytes" : "22914",
"currPkts" : "0",
"currBytes" : "0",
"currIcmp" : "0",
"currUdp" : "0",
"currSyn" : "0",
"currIcmpMax" : "0",
"currUdpMax" : "15",
"currSynMax" : "0"
"ipAddress" : "3232235829",
"macAddress" : "00",:16:E8:22:XX:XX
"totalPkts" : "972",
"totalBytes" : "98730",
"currPkts" : "0",
"currBytes" : "0",
"currIcmp" : "0",
"currUdp" : "0",
"currSyn" : "0",
"currIcmpMax" : "0",
"currUdpMax" : "14",
"currSynMax" : "4"
"ipAddress" : "3232235842",
"macAddress" : "70",:14:A6:8B:XX:XX
"totalPkts" : "154566",
"totalBytes" : "15116490",
"currPkts" : "0",
"currBytes" : "0",
"currIcmp" : "0",
"currUdp" : "0",
"currSyn" : "0",
"currIcmpMax" : "9",
"currUdpMax" : "58",
"currSynMax" : "60"
add a comment |
sed hack:
sed -E -e 's/[([0-9],)5[0-9]][0-9]+/}/g; s/([[:alnum:]]+)=([[:alnum:]]+)/"1" : "2",/;
s/,n/}/; 1s/.*//; $s/.*/&/' yourfile | sed -Ez 's/,n?}/ }n/g'
The output:
"enable" : "1",
"interval" : "10",
"action" : "0"
"ipAddress" : "3232235848",
"macAddress" : "EC",:0E:C4:4F:XX:XX
"totalPkts" : "201",
"totalBytes" : "22914",
"currPkts" : "0",
"currBytes" : "0",
"currIcmp" : "0",
"currUdp" : "0",
"currSyn" : "0",
"currIcmpMax" : "0",
"currUdpMax" : "15",
"currSynMax" : "0"
"ipAddress" : "3232235829",
"macAddress" : "00",:16:E8:22:XX:XX
"totalPkts" : "972",
"totalBytes" : "98730",
"currPkts" : "0",
"currBytes" : "0",
"currIcmp" : "0",
"currUdp" : "0",
"currSyn" : "0",
"currIcmpMax" : "0",
"currUdpMax" : "14",
"currSynMax" : "4"
"ipAddress" : "3232235842",
"macAddress" : "70",:14:A6:8B:XX:XX
"totalPkts" : "154566",
"totalBytes" : "15116490",
"currPkts" : "0",
"currBytes" : "0",
"currIcmp" : "0",
"currUdp" : "0",
"currSyn" : "0",
"currIcmpMax" : "9",
"currUdpMax" : "58",
"currSynMax" : "60"
add a comment |
sed hack:
sed -E -e 's/[([0-9],)5[0-9]][0-9]+/}/g; s/([[:alnum:]]+)=([[:alnum:]]+)/"1" : "2",/;
s/,n/}/; 1s/.*//; $s/.*/&/' yourfile | sed -Ez 's/,n?}/ }n/g'
The output:
"enable" : "1",
"interval" : "10",
"action" : "0"
"ipAddress" : "3232235848",
"macAddress" : "EC",:0E:C4:4F:XX:XX
"totalPkts" : "201",
"totalBytes" : "22914",
"currPkts" : "0",
"currBytes" : "0",
"currIcmp" : "0",
"currUdp" : "0",
"currSyn" : "0",
"currIcmpMax" : "0",
"currUdpMax" : "15",
"currSynMax" : "0"
"ipAddress" : "3232235829",
"macAddress" : "00",:16:E8:22:XX:XX
"totalPkts" : "972",
"totalBytes" : "98730",
"currPkts" : "0",
"currBytes" : "0",
"currIcmp" : "0",
"currUdp" : "0",
"currSyn" : "0",
"currIcmpMax" : "0",
"currUdpMax" : "14",
"currSynMax" : "4"
"ipAddress" : "3232235842",
"macAddress" : "70",:14:A6:8B:XX:XX
"totalPkts" : "154566",
"totalBytes" : "15116490",
"currPkts" : "0",
"currBytes" : "0",
"currIcmp" : "0",
"currUdp" : "0",
"currSyn" : "0",
"currIcmpMax" : "9",
"currUdpMax" : "58",
"currSynMax" : "60"
sed hack:
sed -E -e 's/[([0-9],)5[0-9]][0-9]+/}/g; s/([[:alnum:]]+)=([[:alnum:]]+)/"1" : "2",/;
s/,n/}/; 1s/.*//; $s/.*/&/' yourfile | sed -Ez 's/,n?}/ }n/g'
The output:
"enable" : "1",
"interval" : "10",
"action" : "0"
"ipAddress" : "3232235848",
"macAddress" : "EC",:0E:C4:4F:XX:XX
"totalPkts" : "201",
"totalBytes" : "22914",
"currPkts" : "0",
"currBytes" : "0",
"currIcmp" : "0",
"currUdp" : "0",
"currSyn" : "0",
"currIcmpMax" : "0",
"currUdpMax" : "15",
"currSynMax" : "0"
"ipAddress" : "3232235829",
"macAddress" : "00",:16:E8:22:XX:XX
"totalPkts" : "972",
"totalBytes" : "98730",
"currPkts" : "0",
"currBytes" : "0",
"currIcmp" : "0",
"currUdp" : "0",
"currSyn" : "0",
"currIcmpMax" : "0",
"currUdpMax" : "14",
"currSynMax" : "4"
"ipAddress" : "3232235842",
"macAddress" : "70",:14:A6:8B:XX:XX
"totalPkts" : "154566",
"totalBytes" : "15116490",
"currPkts" : "0",
"currBytes" : "0",
"currIcmp" : "0",
"currUdp" : "0",
"currSyn" : "0",
"currIcmpMax" : "9",
"currUdpMax" : "58",
"currSynMax" : "60"
answered Jun 4 '17 at 21:40
RomanPerekhrestRomanPerekhrest
23.3k12448
23.3k12448
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f369147%2fconvert-text-file-to-json%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown