Minimized JSON code between two patterns with regex

Clash 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]
sed regular-expression json
add a comment |Â
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]
sed regular-expression json
3
Why not usejqor 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 handlejq
â Rui F Ribeiro
Aug 26 at 10:24
add a comment |Â
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]
sed regular-expression json
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
sed regular-expression json
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 usejqor 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 handlejq
â Rui F Ribeiro
Aug 26 at 10:24
add a comment |Â
3
Why not usejqor 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 handlejq
â 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
add a comment |Â
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.
Good answer, +1. Thejqdocumentation is very convoluted, simple examples are more than needed.
â Rui F Ribeiro
Aug 26 at 10:29
add a comment |Â
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.
Good answer, +1. Thejqdocumentation is very convoluted, simple examples are more than needed.
â Rui F Ribeiro
Aug 26 at 10:29
add a comment |Â
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.
Good answer, +1. Thejqdocumentation is very convoluted, simple examples are more than needed.
â Rui F Ribeiro
Aug 26 at 10:29
add a comment |Â
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.
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.
answered Aug 26 at 7:03
Kusalananda
107k14209329
107k14209329
Good answer, +1. Thejqdocumentation is very convoluted, simple examples are more than needed.
â Rui F Ribeiro
Aug 26 at 10:29
add a comment |Â
Good answer, +1. Thejqdocumentation 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
add a comment |Â
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
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
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
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
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
3
Why not use
jqor 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