Minimized JSON code between two patterns with regex

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
-1
down vote

favorite












Let's take a file with for example :



 "test": [


],
"test": [

"name":"bob",
"age":21

]


Is there a way for a regex to minimize the code into something like this :



 "test": [],
"test": ["name":"bob","age":21]









share|improve this question



















  • 3




    Why not use jq or some other appropriate tool? Regular expressions (chomsky type-3) and context-free languages (type-2) like JSON don't mix well.
    – dirkt
    Aug 26 at 5:58










  • The two documents are equivalent when parsed with a JSON parser. Use a JSON parser.
    – Kusalananda
    Aug 26 at 6:33






  • 1




    I would not downvote this question. Been handling a bit of JSON with python, redis and bash lately and it is extremely hard to find simple and clear documentation to handle jq
    – Rui F Ribeiro
    Aug 26 at 10:24















up vote
-1
down vote

favorite












Let's take a file with for example :



 "test": [


],
"test": [

"name":"bob",
"age":21

]


Is there a way for a regex to minimize the code into something like this :



 "test": [],
"test": ["name":"bob","age":21]









share|improve this question



















  • 3




    Why not use jq or some other appropriate tool? Regular expressions (chomsky type-3) and context-free languages (type-2) like JSON don't mix well.
    – dirkt
    Aug 26 at 5:58










  • The two documents are equivalent when parsed with a JSON parser. Use a JSON parser.
    – Kusalananda
    Aug 26 at 6:33






  • 1




    I would not downvote this question. Been handling a bit of JSON with python, redis and bash lately and it is extremely hard to find simple and clear documentation to handle jq
    – Rui F Ribeiro
    Aug 26 at 10:24













up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











Let's take a file with for example :



 "test": [


],
"test": [

"name":"bob",
"age":21

]


Is there a way for a regex to minimize the code into something like this :



 "test": [],
"test": ["name":"bob","age":21]









share|improve this question















Let's take a file with for example :



 "test": [


],
"test": [

"name":"bob",
"age":21

]


Is there a way for a regex to minimize the code into something like this :



 "test": [],
"test": ["name":"bob","age":21]






sed regular-expression json






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 26 at 10:23









Rui F Ribeiro

36.7k1271117




36.7k1271117










asked Aug 26 at 5:01









bob dylan

3001310




3001310







  • 3




    Why not use jq or some other appropriate tool? Regular expressions (chomsky type-3) and context-free languages (type-2) like JSON don't mix well.
    – dirkt
    Aug 26 at 5:58










  • The two documents are equivalent when parsed with a JSON parser. Use a JSON parser.
    – Kusalananda
    Aug 26 at 6:33






  • 1




    I would not downvote this question. Been handling a bit of JSON with python, redis and bash lately and it is extremely hard to find simple and clear documentation to handle jq
    – Rui F Ribeiro
    Aug 26 at 10:24













  • 3




    Why not use jq or some other appropriate tool? Regular expressions (chomsky type-3) and context-free languages (type-2) like JSON don't mix well.
    – dirkt
    Aug 26 at 5:58










  • The two documents are equivalent when parsed with a JSON parser. Use a JSON parser.
    – Kusalananda
    Aug 26 at 6:33






  • 1




    I would not downvote this question. Been handling a bit of JSON with python, redis and bash lately and it is extremely hard to find simple and clear documentation to handle jq
    – Rui F Ribeiro
    Aug 26 at 10:24








3




3




Why not use jq or some other appropriate tool? Regular expressions (chomsky type-3) and context-free languages (type-2) like JSON don't mix well.
– dirkt
Aug 26 at 5:58




Why not use jq or some other appropriate tool? Regular expressions (chomsky type-3) and context-free languages (type-2) like JSON don't mix well.
– dirkt
Aug 26 at 5:58












The two documents are equivalent when parsed with a JSON parser. Use a JSON parser.
– Kusalananda
Aug 26 at 6:33




The two documents are equivalent when parsed with a JSON parser. Use a JSON parser.
– Kusalananda
Aug 26 at 6:33




1




1




I would not downvote this question. Been handling a bit of JSON with python, redis and bash lately and it is extremely hard to find simple and clear documentation to handle jq
– Rui F Ribeiro
Aug 26 at 10:24





I would not downvote this question. Been handling a bit of JSON with python, redis and bash lately and it is extremely hard to find simple and clear documentation to handle jq
– Rui F Ribeiro
Aug 26 at 10:24











1 Answer
1






active

oldest

votes

















up vote
2
down vote













Assuming the two keys test actually are unique (the second test would otherwise override the first test when parsing it with a JSON parser), and that the document is a well formed JSON document, use jq -c to produce a compact version of it.



Here, I've changed the names of the keys so that they are unique, and encapsulated the JSON fragment in an anonymous object:



$ cat file.json

"test1": [

],
"test2": [

"name": "bob",
"age": 21

]


$ jq -c . file.json
"test1":[],"test2":["name":"bob","age":21]


The . on the command line is a jq filter that passes the document through without modifying the contents.






share|improve this answer




















  • Good answer, +1. The jq documentation is very convoluted, simple examples are more than needed.
    – Rui F Ribeiro
    Aug 26 at 10:29











Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f464873%2fminimized-json-code-between-two-patterns-with-regex%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote













Assuming the two keys test actually are unique (the second test would otherwise override the first test when parsing it with a JSON parser), and that the document is a well formed JSON document, use jq -c to produce a compact version of it.



Here, I've changed the names of the keys so that they are unique, and encapsulated the JSON fragment in an anonymous object:



$ cat file.json

"test1": [

],
"test2": [

"name": "bob",
"age": 21

]


$ jq -c . file.json
"test1":[],"test2":["name":"bob","age":21]


The . on the command line is a jq filter that passes the document through without modifying the contents.






share|improve this answer




















  • Good answer, +1. The jq documentation is very convoluted, simple examples are more than needed.
    – Rui F Ribeiro
    Aug 26 at 10:29















up vote
2
down vote













Assuming the two keys test actually are unique (the second test would otherwise override the first test when parsing it with a JSON parser), and that the document is a well formed JSON document, use jq -c to produce a compact version of it.



Here, I've changed the names of the keys so that they are unique, and encapsulated the JSON fragment in an anonymous object:



$ cat file.json

"test1": [

],
"test2": [

"name": "bob",
"age": 21

]


$ jq -c . file.json
"test1":[],"test2":["name":"bob","age":21]


The . on the command line is a jq filter that passes the document through without modifying the contents.






share|improve this answer




















  • Good answer, +1. The jq documentation is very convoluted, simple examples are more than needed.
    – Rui F Ribeiro
    Aug 26 at 10:29













up vote
2
down vote










up vote
2
down vote









Assuming the two keys test actually are unique (the second test would otherwise override the first test when parsing it with a JSON parser), and that the document is a well formed JSON document, use jq -c to produce a compact version of it.



Here, I've changed the names of the keys so that they are unique, and encapsulated the JSON fragment in an anonymous object:



$ cat file.json

"test1": [

],
"test2": [

"name": "bob",
"age": 21

]


$ jq -c . file.json
"test1":[],"test2":["name":"bob","age":21]


The . on the command line is a jq filter that passes the document through without modifying the contents.






share|improve this answer












Assuming the two keys test actually are unique (the second test would otherwise override the first test when parsing it with a JSON parser), and that the document is a well formed JSON document, use jq -c to produce a compact version of it.



Here, I've changed the names of the keys so that they are unique, and encapsulated the JSON fragment in an anonymous object:



$ cat file.json

"test1": [

],
"test2": [

"name": "bob",
"age": 21

]


$ jq -c . file.json
"test1":[],"test2":["name":"bob","age":21]


The . on the command line is a jq filter that passes the document through without modifying the contents.







share|improve this answer












share|improve this answer



share|improve this answer










answered Aug 26 at 7:03









Kusalananda

107k14209329




107k14209329











  • Good answer, +1. The jq documentation is very convoluted, simple examples are more than needed.
    – Rui F Ribeiro
    Aug 26 at 10:29

















  • Good answer, +1. The jq documentation is very convoluted, simple examples are more than needed.
    – Rui F Ribeiro
    Aug 26 at 10:29
















Good answer, +1. The jq documentation is very convoluted, simple examples are more than needed.
– Rui F Ribeiro
Aug 26 at 10:29





Good answer, +1. The jq documentation is very convoluted, simple examples are more than needed.
– Rui F Ribeiro
Aug 26 at 10:29


















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f464873%2fminimized-json-code-between-two-patterns-with-regex%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

Peggy Mitchell

Palaiologos

The Forum (Inglewood, California)