Find and replace inside string based on pattern and exception cases
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
I have a JSON string that returns some key value pairs but if the value is numeric it doesn't enclose it with double quotes so my JSON parsing isn't working for those cases.
My idea is to manipulate the string by searching and replacing any occurrences where it finds :
with :"
except when the : is followed by an open curly brace {
I would also need to close the double quotes at the end so I need to find a way to look for any 0-9 followed by a comma and replace that comma with ",
I am thinking I need SED to search/replace but I am very new to SED and RegEx so not sure how to start especially omitting places where it finds "{
and using RegEx to find [0-9],
Sample JSON string looks like:
"data":"project":"issue":"session":"id":"625fdv6b95e232f08d6cy2686624f315","createdAt":1539849060000,"buildVersionId":"75492373","sdk":"display":"1.11.1","os":"platform":"unknown","modified":false,"memory":"free":853000192,"used":1896611840,"storage":"free":241791528960,"used":14197940224,"device":"architecture":"arm64","manufacturer":"Samsung"
Desired finished JSON after the change would look like:
"data":"project":"issue":"session":"id":"625fdv6b95e232f08d6cy2686624f315","createdAt":"1539849060000","buildVersionId":"75492373","sdk":"display":"1.11.1","os":"platform":"unknown","modified":false,"memory":"free":"853000192","used":"1896611840","storage":"free":"241791528960","used":"14197940224","device":"architecture":"arm64","manufacturer":"Samsung"
pretty much the same thing but every number now has double quotes around it.
Any help would be appreciated!
sed json
New contributor
add a comment |Â
up vote
0
down vote
favorite
I have a JSON string that returns some key value pairs but if the value is numeric it doesn't enclose it with double quotes so my JSON parsing isn't working for those cases.
My idea is to manipulate the string by searching and replacing any occurrences where it finds :
with :"
except when the : is followed by an open curly brace {
I would also need to close the double quotes at the end so I need to find a way to look for any 0-9 followed by a comma and replace that comma with ",
I am thinking I need SED to search/replace but I am very new to SED and RegEx so not sure how to start especially omitting places where it finds "{
and using RegEx to find [0-9],
Sample JSON string looks like:
"data":"project":"issue":"session":"id":"625fdv6b95e232f08d6cy2686624f315","createdAt":1539849060000,"buildVersionId":"75492373","sdk":"display":"1.11.1","os":"platform":"unknown","modified":false,"memory":"free":853000192,"used":1896611840,"storage":"free":241791528960,"used":14197940224,"device":"architecture":"arm64","manufacturer":"Samsung"
Desired finished JSON after the change would look like:
"data":"project":"issue":"session":"id":"625fdv6b95e232f08d6cy2686624f315","createdAt":"1539849060000","buildVersionId":"75492373","sdk":"display":"1.11.1","os":"platform":"unknown","modified":false,"memory":"free":"853000192","used":"1896611840","storage":"free":"241791528960","used":"14197940224","device":"architecture":"arm64","manufacturer":"Samsung"
pretty much the same thing but every number now has double quotes around it.
Any help would be appreciated!
sed json
New contributor
1
Welcome to U&L. Could you try adding a sample 'desired output' string ?
â steve
39 mins ago
1
Hi Steve, I edited my question to have the desired output. Thanks!
â ACCFAN
34 mins ago
I see some evidence that JSON numbers should not be quoted.
â Jeff Schaller
23 mins ago
Jeff, yes, I understand numbers won't be numeric anymore and will be considered strings but that won't affect what my end result will be so it doesn't matter for me. I won't be sending this edited string to anything else for processing. It's only used to parse and extract data out of it. Thanks
â ACCFAN
20 mins ago
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a JSON string that returns some key value pairs but if the value is numeric it doesn't enclose it with double quotes so my JSON parsing isn't working for those cases.
My idea is to manipulate the string by searching and replacing any occurrences where it finds :
with :"
except when the : is followed by an open curly brace {
I would also need to close the double quotes at the end so I need to find a way to look for any 0-9 followed by a comma and replace that comma with ",
I am thinking I need SED to search/replace but I am very new to SED and RegEx so not sure how to start especially omitting places where it finds "{
and using RegEx to find [0-9],
Sample JSON string looks like:
"data":"project":"issue":"session":"id":"625fdv6b95e232f08d6cy2686624f315","createdAt":1539849060000,"buildVersionId":"75492373","sdk":"display":"1.11.1","os":"platform":"unknown","modified":false,"memory":"free":853000192,"used":1896611840,"storage":"free":241791528960,"used":14197940224,"device":"architecture":"arm64","manufacturer":"Samsung"
Desired finished JSON after the change would look like:
"data":"project":"issue":"session":"id":"625fdv6b95e232f08d6cy2686624f315","createdAt":"1539849060000","buildVersionId":"75492373","sdk":"display":"1.11.1","os":"platform":"unknown","modified":false,"memory":"free":"853000192","used":"1896611840","storage":"free":"241791528960","used":"14197940224","device":"architecture":"arm64","manufacturer":"Samsung"
pretty much the same thing but every number now has double quotes around it.
Any help would be appreciated!
sed json
New contributor
I have a JSON string that returns some key value pairs but if the value is numeric it doesn't enclose it with double quotes so my JSON parsing isn't working for those cases.
My idea is to manipulate the string by searching and replacing any occurrences where it finds :
with :"
except when the : is followed by an open curly brace {
I would also need to close the double quotes at the end so I need to find a way to look for any 0-9 followed by a comma and replace that comma with ",
I am thinking I need SED to search/replace but I am very new to SED and RegEx so not sure how to start especially omitting places where it finds "{
and using RegEx to find [0-9],
Sample JSON string looks like:
"data":"project":"issue":"session":"id":"625fdv6b95e232f08d6cy2686624f315","createdAt":1539849060000,"buildVersionId":"75492373","sdk":"display":"1.11.1","os":"platform":"unknown","modified":false,"memory":"free":853000192,"used":1896611840,"storage":"free":241791528960,"used":14197940224,"device":"architecture":"arm64","manufacturer":"Samsung"
Desired finished JSON after the change would look like:
"data":"project":"issue":"session":"id":"625fdv6b95e232f08d6cy2686624f315","createdAt":"1539849060000","buildVersionId":"75492373","sdk":"display":"1.11.1","os":"platform":"unknown","modified":false,"memory":"free":"853000192","used":"1896611840","storage":"free":"241791528960","used":"14197940224","device":"architecture":"arm64","manufacturer":"Samsung"
pretty much the same thing but every number now has double quotes around it.
Any help would be appreciated!
sed json
sed json
New contributor
New contributor
edited 35 mins ago
New contributor
asked 42 mins ago
ACCFAN
11
11
New contributor
New contributor
1
Welcome to U&L. Could you try adding a sample 'desired output' string ?
â steve
39 mins ago
1
Hi Steve, I edited my question to have the desired output. Thanks!
â ACCFAN
34 mins ago
I see some evidence that JSON numbers should not be quoted.
â Jeff Schaller
23 mins ago
Jeff, yes, I understand numbers won't be numeric anymore and will be considered strings but that won't affect what my end result will be so it doesn't matter for me. I won't be sending this edited string to anything else for processing. It's only used to parse and extract data out of it. Thanks
â ACCFAN
20 mins ago
add a comment |Â
1
Welcome to U&L. Could you try adding a sample 'desired output' string ?
â steve
39 mins ago
1
Hi Steve, I edited my question to have the desired output. Thanks!
â ACCFAN
34 mins ago
I see some evidence that JSON numbers should not be quoted.
â Jeff Schaller
23 mins ago
Jeff, yes, I understand numbers won't be numeric anymore and will be considered strings but that won't affect what my end result will be so it doesn't matter for me. I won't be sending this edited string to anything else for processing. It's only used to parse and extract data out of it. Thanks
â ACCFAN
20 mins ago
1
1
Welcome to U&L. Could you try adding a sample 'desired output' string ?
â steve
39 mins ago
Welcome to U&L. Could you try adding a sample 'desired output' string ?
â steve
39 mins ago
1
1
Hi Steve, I edited my question to have the desired output. Thanks!
â ACCFAN
34 mins ago
Hi Steve, I edited my question to have the desired output. Thanks!
â ACCFAN
34 mins ago
I see some evidence that JSON numbers should not be quoted.
â Jeff Schaller
23 mins ago
I see some evidence that JSON numbers should not be quoted.
â Jeff Schaller
23 mins ago
Jeff, yes, I understand numbers won't be numeric anymore and will be considered strings but that won't affect what my end result will be so it doesn't matter for me. I won't be sending this edited string to anything else for processing. It's only used to parse and extract data out of it. Thanks
â ACCFAN
20 mins ago
Jeff, yes, I understand numbers won't be numeric anymore and will be considered strings but that won't affect what my end result will be so it doesn't matter for me. I won't be sending this edited string to anything else for processing. It's only used to parse and extract data out of it. Thanks
â ACCFAN
20 mins ago
add a comment |Â
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
ACCFAN is a new contributor. Be nice, and check out our Code of Conduct.
ACCFAN is a new contributor. Be nice, and check out our Code of Conduct.
ACCFAN is a new contributor. Be nice, and check out our Code of Conduct.
ACCFAN is a new contributor. Be nice, and check out our Code of Conduct.
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%2f477594%2ffind-and-replace-inside-string-based-on-pattern-and-exception-cases%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
1
Welcome to U&L. Could you try adding a sample 'desired output' string ?
â steve
39 mins ago
1
Hi Steve, I edited my question to have the desired output. Thanks!
â ACCFAN
34 mins ago
I see some evidence that JSON numbers should not be quoted.
â Jeff Schaller
23 mins ago
Jeff, yes, I understand numbers won't be numeric anymore and will be considered strings but that won't affect what my end result will be so it doesn't matter for me. I won't be sending this edited string to anything else for processing. It's only used to parse and extract data out of it. Thanks
â ACCFAN
20 mins ago