Find and replace inside string based on pattern and exception cases

The name of the pictureThe name of the pictureThe name of the pictureClash 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!










share|improve this question









New contributor




ACCFAN is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 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














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!










share|improve this question









New contributor




ACCFAN is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 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












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!










share|improve this question









New contributor




ACCFAN is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











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






share|improve this question









New contributor




ACCFAN is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




ACCFAN is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 35 mins ago





















New contributor




ACCFAN is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 42 mins ago









ACCFAN

11




11




New contributor




ACCFAN is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





ACCFAN is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






ACCFAN is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







  • 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




    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















active

oldest

votes











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
);



);






ACCFAN is a new contributor. Be nice, and check out our Code of Conduct.









 

draft saved


draft discarded


















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



































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.









 

draft saved


draft discarded


















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.













 


draft saved


draft discarded














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













































































Popular posts from this blog

How to check contact read email or not when send email to Individual?

Bahrain

Postfix configuration issue with fips on centos 7; mailgun relay