Wrap all numerics in JSON with quotes
There are JSON data which contains some numeric values. How to convert all numerics to strings? (wrap with quotes)
Example:
{
"id":1,
"customer":"user",
"plate":"BMT-216-A",
"country":"GB",
"amount":1000,
"pndNumber":20000,
"zoneNumber":4
}
should become
{
"id":"1",
"customer":"user",
"plate":"BMT-216-A",
"country":"GB",
"amount":"1000",
"pndNumber":"20000",
"zoneNumber":"4"
}
shell json jq
add a comment |
There are JSON data which contains some numeric values. How to convert all numerics to strings? (wrap with quotes)
Example:
{
"id":1,
"customer":"user",
"plate":"BMT-216-A",
"country":"GB",
"amount":1000,
"pndNumber":20000,
"zoneNumber":4
}
should become
{
"id":"1",
"customer":"user",
"plate":"BMT-216-A",
"country":"GB",
"amount":"1000",
"pndNumber":"20000",
"zoneNumber":"4"
}
shell json jq
add a comment |
There are JSON data which contains some numeric values. How to convert all numerics to strings? (wrap with quotes)
Example:
{
"id":1,
"customer":"user",
"plate":"BMT-216-A",
"country":"GB",
"amount":1000,
"pndNumber":20000,
"zoneNumber":4
}
should become
{
"id":"1",
"customer":"user",
"plate":"BMT-216-A",
"country":"GB",
"amount":"1000",
"pndNumber":"20000",
"zoneNumber":"4"
}
shell json jq
There are JSON data which contains some numeric values. How to convert all numerics to strings? (wrap with quotes)
Example:
{
"id":1,
"customer":"user",
"plate":"BMT-216-A",
"country":"GB",
"amount":1000,
"pndNumber":20000,
"zoneNumber":4
}
should become
{
"id":"1",
"customer":"user",
"plate":"BMT-216-A",
"country":"GB",
"amount":"1000",
"pndNumber":"20000",
"zoneNumber":"4"
}
shell json jq
shell json jq
edited yesterday
V-K
asked yesterday
V-KV-K
1637
1637
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
$ jq 'map_values(tostring)' file.json
{
"id": "1",
"customer": "user",
"plate": "BMT-216-A",
"country": "GB",
"amount": "1000",
"pndNumber": "20000",
"zoneNumber": "4"
}
Redirect to a new file and then move that to the original filename.
For a more thorough conversion of numbers in non-flat structures into strings, consider
jq '(..|select(type == "number")) |= tostring' file.json
This would examine every value recursively in the given document, and select the ones that are numbers. The selected values are then converted into strings. It would also, strictly speaking, look at the keys, but since these can't be plain numbers in JSON, no key would be selected.
Example:
$ jq . file.json
{
"a": {
"b": 1
},
"b": null,
"c": [
1,
2,
"hello",
4
]
}
$ jq '(..|select(type == "number")) |= tostring' file.json
{
"a": {
"b": "1"
},
"b": null,
"c": [
"1",
"2",
"hello",
"4"
]
}
To additionally quote the null
, change the select()
to
select(type == "number" or type == "null")
3
Note that it changes{"a":{"b":1},"b":null}
to{ "a": "{"b":1}", "b": "null" }
– Stéphane Chazelas
yesterday
@StéphaneChazelas Yes, it would turn sub-objects into strings. The given data structure does not however contains sub-objects.
– Kusalananda
yesterday
2
Not only sub-objects, all values including arrays, booleans andnull
(still worth noting IMO even though the OP's sample has none of those).
– Stéphane Chazelas
yesterday
And how to change this if I have an array?
– V-K
yesterday
@StéphaneChazelas Sorted. Thanks for poking at me.
– Kusalananda
yesterday
add a comment |
here's an easy solution based on jtc
unix utility:
bash $ jtc -w'<.*>D:' -eu echo '"{}"' ; file.json
{
"amount": "1000",
"country": "GB",
"customer": "user",
"id": "1",
"plate": "BMT-216-A",
"pndNumber": "20000",
"zoneNumber": "4"
}
bash $
if you like to apply changes right into the json file, use the -f
switch, like this:
bash $ jtc -f -w'<.*>D:' -eu echo '"{}"' ; file.json
The proposed solution will work correctly with an arbitrary structured jsons, e.g.:
bash $ jtc -w'<.*>D:' -eu echo '"{}"' ; file.json
{
"amount": "1000",
"country": "GB",
"customer": "user",
"id": "1",
"plate": "BMT-216-A",
"pndNumber": "20000",
"sub": {
"subvalue": "123"
},
"zoneNumber": "4"
}
bash $
- if you like to quote null values, just throw in a walk-path
-w'<>n:'
- if you like to quote boolean values, throw in a walk-path
-w'<any>b:'
Also, the reverse task (unquote all the numerics) is easily achieved in the similar way: say, file.json
is already "enquoted", to unquote all the numerics:
bash $ jtc -w'<^d+$>R:' -eu echo {-} ; file.json
{
"amount": 1000,
"country": "GB",
"customer": "user",
"id": 1,
"plate": "BMT-216-A",
"pndNumber": 20000,
"zoneNumber": 4
}
bash $
jtc
user guide: https://github.com/ldn-softdev/jtc/blob/master/User%20Guide.md
New contributor
add a comment |
perl -pe 's/("(?:\.|[^"])*")|[^s[]{}:,"]+/$1||qq("$&")/ge' file.json
Would quote anything that is not quoted and is not {}:,whitespace
, so would quote numbers, true
, false
and null
.
perl -pe 's/("(?:\.|[^"])*")|-?d+(?:.d+)?(?:[eE][-+]?d+)?/$1||qq("$&")/ge'
Would specifically quote what matches the specification of a json number and that is not already inside quotes.
Those do an exact tokenising based on the JSON specification, it's not an approximation.
add a comment |
I tried with below method and it worked fine.
I pipelined 2 times tried up to my level to reduce it
Command:
sed 's/[0-9]{1,},?$/"&/g' filename |
sed '/[0-9]{1,}$/s/[0-9]{1,}/&"/g'|
sed '/[0-9]{1,},$/s/,$/"&/g`'
Output:
{
"id":"1",
"customer":"user",
"plate":"BMT-216-A",
"country":"GB",
"amount":"1000",
"pndNumber":"20000",
"zoneNumber":"4"
}
@Kusalananda corrected the code
– Praveen Kumar BS
yesterday
why do you use{1,},
? To test whether an element appears one or more times use+
. And this won't work for numbers like -123, 0xab, 0o12, 0b1011, 1e23 or 1.2e3...
– phuclv
23 hours ago
@phuclv{1,}
is the BRE equivalent of ERE+
. Somesed
implementations support+
as an extension or a-E
or-r
option to enable EREs but that's not portable.?
is another non-portable extension though whose standard equivalent is{0,1}
– Stéphane Chazelas
22 hours ago
@phuclv you wouldn't find unquoted 0xab 0o12 0b1011 numbers in a valid JSON file.
– Stéphane Chazelas
22 hours ago
add a comment |
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',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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%2f504444%2fwrap-all-numerics-in-json-with-quotes%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
$ jq 'map_values(tostring)' file.json
{
"id": "1",
"customer": "user",
"plate": "BMT-216-A",
"country": "GB",
"amount": "1000",
"pndNumber": "20000",
"zoneNumber": "4"
}
Redirect to a new file and then move that to the original filename.
For a more thorough conversion of numbers in non-flat structures into strings, consider
jq '(..|select(type == "number")) |= tostring' file.json
This would examine every value recursively in the given document, and select the ones that are numbers. The selected values are then converted into strings. It would also, strictly speaking, look at the keys, but since these can't be plain numbers in JSON, no key would be selected.
Example:
$ jq . file.json
{
"a": {
"b": 1
},
"b": null,
"c": [
1,
2,
"hello",
4
]
}
$ jq '(..|select(type == "number")) |= tostring' file.json
{
"a": {
"b": "1"
},
"b": null,
"c": [
"1",
"2",
"hello",
"4"
]
}
To additionally quote the null
, change the select()
to
select(type == "number" or type == "null")
3
Note that it changes{"a":{"b":1},"b":null}
to{ "a": "{"b":1}", "b": "null" }
– Stéphane Chazelas
yesterday
@StéphaneChazelas Yes, it would turn sub-objects into strings. The given data structure does not however contains sub-objects.
– Kusalananda
yesterday
2
Not only sub-objects, all values including arrays, booleans andnull
(still worth noting IMO even though the OP's sample has none of those).
– Stéphane Chazelas
yesterday
And how to change this if I have an array?
– V-K
yesterday
@StéphaneChazelas Sorted. Thanks for poking at me.
– Kusalananda
yesterday
add a comment |
$ jq 'map_values(tostring)' file.json
{
"id": "1",
"customer": "user",
"plate": "BMT-216-A",
"country": "GB",
"amount": "1000",
"pndNumber": "20000",
"zoneNumber": "4"
}
Redirect to a new file and then move that to the original filename.
For a more thorough conversion of numbers in non-flat structures into strings, consider
jq '(..|select(type == "number")) |= tostring' file.json
This would examine every value recursively in the given document, and select the ones that are numbers. The selected values are then converted into strings. It would also, strictly speaking, look at the keys, but since these can't be plain numbers in JSON, no key would be selected.
Example:
$ jq . file.json
{
"a": {
"b": 1
},
"b": null,
"c": [
1,
2,
"hello",
4
]
}
$ jq '(..|select(type == "number")) |= tostring' file.json
{
"a": {
"b": "1"
},
"b": null,
"c": [
"1",
"2",
"hello",
"4"
]
}
To additionally quote the null
, change the select()
to
select(type == "number" or type == "null")
3
Note that it changes{"a":{"b":1},"b":null}
to{ "a": "{"b":1}", "b": "null" }
– Stéphane Chazelas
yesterday
@StéphaneChazelas Yes, it would turn sub-objects into strings. The given data structure does not however contains sub-objects.
– Kusalananda
yesterday
2
Not only sub-objects, all values including arrays, booleans andnull
(still worth noting IMO even though the OP's sample has none of those).
– Stéphane Chazelas
yesterday
And how to change this if I have an array?
– V-K
yesterday
@StéphaneChazelas Sorted. Thanks for poking at me.
– Kusalananda
yesterday
add a comment |
$ jq 'map_values(tostring)' file.json
{
"id": "1",
"customer": "user",
"plate": "BMT-216-A",
"country": "GB",
"amount": "1000",
"pndNumber": "20000",
"zoneNumber": "4"
}
Redirect to a new file and then move that to the original filename.
For a more thorough conversion of numbers in non-flat structures into strings, consider
jq '(..|select(type == "number")) |= tostring' file.json
This would examine every value recursively in the given document, and select the ones that are numbers. The selected values are then converted into strings. It would also, strictly speaking, look at the keys, but since these can't be plain numbers in JSON, no key would be selected.
Example:
$ jq . file.json
{
"a": {
"b": 1
},
"b": null,
"c": [
1,
2,
"hello",
4
]
}
$ jq '(..|select(type == "number")) |= tostring' file.json
{
"a": {
"b": "1"
},
"b": null,
"c": [
"1",
"2",
"hello",
"4"
]
}
To additionally quote the null
, change the select()
to
select(type == "number" or type == "null")
$ jq 'map_values(tostring)' file.json
{
"id": "1",
"customer": "user",
"plate": "BMT-216-A",
"country": "GB",
"amount": "1000",
"pndNumber": "20000",
"zoneNumber": "4"
}
Redirect to a new file and then move that to the original filename.
For a more thorough conversion of numbers in non-flat structures into strings, consider
jq '(..|select(type == "number")) |= tostring' file.json
This would examine every value recursively in the given document, and select the ones that are numbers. The selected values are then converted into strings. It would also, strictly speaking, look at the keys, but since these can't be plain numbers in JSON, no key would be selected.
Example:
$ jq . file.json
{
"a": {
"b": 1
},
"b": null,
"c": [
1,
2,
"hello",
4
]
}
$ jq '(..|select(type == "number")) |= tostring' file.json
{
"a": {
"b": "1"
},
"b": null,
"c": [
"1",
"2",
"hello",
"4"
]
}
To additionally quote the null
, change the select()
to
select(type == "number" or type == "null")
edited yesterday
answered yesterday
KusalanandaKusalananda
134k17255418
134k17255418
3
Note that it changes{"a":{"b":1},"b":null}
to{ "a": "{"b":1}", "b": "null" }
– Stéphane Chazelas
yesterday
@StéphaneChazelas Yes, it would turn sub-objects into strings. The given data structure does not however contains sub-objects.
– Kusalananda
yesterday
2
Not only sub-objects, all values including arrays, booleans andnull
(still worth noting IMO even though the OP's sample has none of those).
– Stéphane Chazelas
yesterday
And how to change this if I have an array?
– V-K
yesterday
@StéphaneChazelas Sorted. Thanks for poking at me.
– Kusalananda
yesterday
add a comment |
3
Note that it changes{"a":{"b":1},"b":null}
to{ "a": "{"b":1}", "b": "null" }
– Stéphane Chazelas
yesterday
@StéphaneChazelas Yes, it would turn sub-objects into strings. The given data structure does not however contains sub-objects.
– Kusalananda
yesterday
2
Not only sub-objects, all values including arrays, booleans andnull
(still worth noting IMO even though the OP's sample has none of those).
– Stéphane Chazelas
yesterday
And how to change this if I have an array?
– V-K
yesterday
@StéphaneChazelas Sorted. Thanks for poking at me.
– Kusalananda
yesterday
3
3
Note that it changes
{"a":{"b":1},"b":null}
to { "a": "{"b":1}", "b": "null" }
– Stéphane Chazelas
yesterday
Note that it changes
{"a":{"b":1},"b":null}
to { "a": "{"b":1}", "b": "null" }
– Stéphane Chazelas
yesterday
@StéphaneChazelas Yes, it would turn sub-objects into strings. The given data structure does not however contains sub-objects.
– Kusalananda
yesterday
@StéphaneChazelas Yes, it would turn sub-objects into strings. The given data structure does not however contains sub-objects.
– Kusalananda
yesterday
2
2
Not only sub-objects, all values including arrays, booleans and
null
(still worth noting IMO even though the OP's sample has none of those).– Stéphane Chazelas
yesterday
Not only sub-objects, all values including arrays, booleans and
null
(still worth noting IMO even though the OP's sample has none of those).– Stéphane Chazelas
yesterday
And how to change this if I have an array?
– V-K
yesterday
And how to change this if I have an array?
– V-K
yesterday
@StéphaneChazelas Sorted. Thanks for poking at me.
– Kusalananda
yesterday
@StéphaneChazelas Sorted. Thanks for poking at me.
– Kusalananda
yesterday
add a comment |
here's an easy solution based on jtc
unix utility:
bash $ jtc -w'<.*>D:' -eu echo '"{}"' ; file.json
{
"amount": "1000",
"country": "GB",
"customer": "user",
"id": "1",
"plate": "BMT-216-A",
"pndNumber": "20000",
"zoneNumber": "4"
}
bash $
if you like to apply changes right into the json file, use the -f
switch, like this:
bash $ jtc -f -w'<.*>D:' -eu echo '"{}"' ; file.json
The proposed solution will work correctly with an arbitrary structured jsons, e.g.:
bash $ jtc -w'<.*>D:' -eu echo '"{}"' ; file.json
{
"amount": "1000",
"country": "GB",
"customer": "user",
"id": "1",
"plate": "BMT-216-A",
"pndNumber": "20000",
"sub": {
"subvalue": "123"
},
"zoneNumber": "4"
}
bash $
- if you like to quote null values, just throw in a walk-path
-w'<>n:'
- if you like to quote boolean values, throw in a walk-path
-w'<any>b:'
Also, the reverse task (unquote all the numerics) is easily achieved in the similar way: say, file.json
is already "enquoted", to unquote all the numerics:
bash $ jtc -w'<^d+$>R:' -eu echo {-} ; file.json
{
"amount": 1000,
"country": "GB",
"customer": "user",
"id": 1,
"plate": "BMT-216-A",
"pndNumber": 20000,
"zoneNumber": 4
}
bash $
jtc
user guide: https://github.com/ldn-softdev/jtc/blob/master/User%20Guide.md
New contributor
add a comment |
here's an easy solution based on jtc
unix utility:
bash $ jtc -w'<.*>D:' -eu echo '"{}"' ; file.json
{
"amount": "1000",
"country": "GB",
"customer": "user",
"id": "1",
"plate": "BMT-216-A",
"pndNumber": "20000",
"zoneNumber": "4"
}
bash $
if you like to apply changes right into the json file, use the -f
switch, like this:
bash $ jtc -f -w'<.*>D:' -eu echo '"{}"' ; file.json
The proposed solution will work correctly with an arbitrary structured jsons, e.g.:
bash $ jtc -w'<.*>D:' -eu echo '"{}"' ; file.json
{
"amount": "1000",
"country": "GB",
"customer": "user",
"id": "1",
"plate": "BMT-216-A",
"pndNumber": "20000",
"sub": {
"subvalue": "123"
},
"zoneNumber": "4"
}
bash $
- if you like to quote null values, just throw in a walk-path
-w'<>n:'
- if you like to quote boolean values, throw in a walk-path
-w'<any>b:'
Also, the reverse task (unquote all the numerics) is easily achieved in the similar way: say, file.json
is already "enquoted", to unquote all the numerics:
bash $ jtc -w'<^d+$>R:' -eu echo {-} ; file.json
{
"amount": 1000,
"country": "GB",
"customer": "user",
"id": 1,
"plate": "BMT-216-A",
"pndNumber": 20000,
"zoneNumber": 4
}
bash $
jtc
user guide: https://github.com/ldn-softdev/jtc/blob/master/User%20Guide.md
New contributor
add a comment |
here's an easy solution based on jtc
unix utility:
bash $ jtc -w'<.*>D:' -eu echo '"{}"' ; file.json
{
"amount": "1000",
"country": "GB",
"customer": "user",
"id": "1",
"plate": "BMT-216-A",
"pndNumber": "20000",
"zoneNumber": "4"
}
bash $
if you like to apply changes right into the json file, use the -f
switch, like this:
bash $ jtc -f -w'<.*>D:' -eu echo '"{}"' ; file.json
The proposed solution will work correctly with an arbitrary structured jsons, e.g.:
bash $ jtc -w'<.*>D:' -eu echo '"{}"' ; file.json
{
"amount": "1000",
"country": "GB",
"customer": "user",
"id": "1",
"plate": "BMT-216-A",
"pndNumber": "20000",
"sub": {
"subvalue": "123"
},
"zoneNumber": "4"
}
bash $
- if you like to quote null values, just throw in a walk-path
-w'<>n:'
- if you like to quote boolean values, throw in a walk-path
-w'<any>b:'
Also, the reverse task (unquote all the numerics) is easily achieved in the similar way: say, file.json
is already "enquoted", to unquote all the numerics:
bash $ jtc -w'<^d+$>R:' -eu echo {-} ; file.json
{
"amount": 1000,
"country": "GB",
"customer": "user",
"id": 1,
"plate": "BMT-216-A",
"pndNumber": 20000,
"zoneNumber": 4
}
bash $
jtc
user guide: https://github.com/ldn-softdev/jtc/blob/master/User%20Guide.md
New contributor
here's an easy solution based on jtc
unix utility:
bash $ jtc -w'<.*>D:' -eu echo '"{}"' ; file.json
{
"amount": "1000",
"country": "GB",
"customer": "user",
"id": "1",
"plate": "BMT-216-A",
"pndNumber": "20000",
"zoneNumber": "4"
}
bash $
if you like to apply changes right into the json file, use the -f
switch, like this:
bash $ jtc -f -w'<.*>D:' -eu echo '"{}"' ; file.json
The proposed solution will work correctly with an arbitrary structured jsons, e.g.:
bash $ jtc -w'<.*>D:' -eu echo '"{}"' ; file.json
{
"amount": "1000",
"country": "GB",
"customer": "user",
"id": "1",
"plate": "BMT-216-A",
"pndNumber": "20000",
"sub": {
"subvalue": "123"
},
"zoneNumber": "4"
}
bash $
- if you like to quote null values, just throw in a walk-path
-w'<>n:'
- if you like to quote boolean values, throw in a walk-path
-w'<any>b:'
Also, the reverse task (unquote all the numerics) is easily achieved in the similar way: say, file.json
is already "enquoted", to unquote all the numerics:
bash $ jtc -w'<^d+$>R:' -eu echo {-} ; file.json
{
"amount": 1000,
"country": "GB",
"customer": "user",
"id": 1,
"plate": "BMT-216-A",
"pndNumber": 20000,
"zoneNumber": 4
}
bash $
jtc
user guide: https://github.com/ldn-softdev/jtc/blob/master/User%20Guide.md
New contributor
edited yesterday
michaelb958
10315
10315
New contributor
answered yesterday
DmitryDmitry
812
812
New contributor
New contributor
add a comment |
add a comment |
perl -pe 's/("(?:\.|[^"])*")|[^s[]{}:,"]+/$1||qq("$&")/ge' file.json
Would quote anything that is not quoted and is not {}:,whitespace
, so would quote numbers, true
, false
and null
.
perl -pe 's/("(?:\.|[^"])*")|-?d+(?:.d+)?(?:[eE][-+]?d+)?/$1||qq("$&")/ge'
Would specifically quote what matches the specification of a json number and that is not already inside quotes.
Those do an exact tokenising based on the JSON specification, it's not an approximation.
add a comment |
perl -pe 's/("(?:\.|[^"])*")|[^s[]{}:,"]+/$1||qq("$&")/ge' file.json
Would quote anything that is not quoted and is not {}:,whitespace
, so would quote numbers, true
, false
and null
.
perl -pe 's/("(?:\.|[^"])*")|-?d+(?:.d+)?(?:[eE][-+]?d+)?/$1||qq("$&")/ge'
Would specifically quote what matches the specification of a json number and that is not already inside quotes.
Those do an exact tokenising based on the JSON specification, it's not an approximation.
add a comment |
perl -pe 's/("(?:\.|[^"])*")|[^s[]{}:,"]+/$1||qq("$&")/ge' file.json
Would quote anything that is not quoted and is not {}:,whitespace
, so would quote numbers, true
, false
and null
.
perl -pe 's/("(?:\.|[^"])*")|-?d+(?:.d+)?(?:[eE][-+]?d+)?/$1||qq("$&")/ge'
Would specifically quote what matches the specification of a json number and that is not already inside quotes.
Those do an exact tokenising based on the JSON specification, it's not an approximation.
perl -pe 's/("(?:\.|[^"])*")|[^s[]{}:,"]+/$1||qq("$&")/ge' file.json
Would quote anything that is not quoted and is not {}:,whitespace
, so would quote numbers, true
, false
and null
.
perl -pe 's/("(?:\.|[^"])*")|-?d+(?:.d+)?(?:[eE][-+]?d+)?/$1||qq("$&")/ge'
Would specifically quote what matches the specification of a json number and that is not already inside quotes.
Those do an exact tokenising based on the JSON specification, it's not an approximation.
edited yesterday
answered yesterday
Stéphane ChazelasStéphane Chazelas
308k57582940
308k57582940
add a comment |
add a comment |
I tried with below method and it worked fine.
I pipelined 2 times tried up to my level to reduce it
Command:
sed 's/[0-9]{1,},?$/"&/g' filename |
sed '/[0-9]{1,}$/s/[0-9]{1,}/&"/g'|
sed '/[0-9]{1,},$/s/,$/"&/g`'
Output:
{
"id":"1",
"customer":"user",
"plate":"BMT-216-A",
"country":"GB",
"amount":"1000",
"pndNumber":"20000",
"zoneNumber":"4"
}
@Kusalananda corrected the code
– Praveen Kumar BS
yesterday
why do you use{1,},
? To test whether an element appears one or more times use+
. And this won't work for numbers like -123, 0xab, 0o12, 0b1011, 1e23 or 1.2e3...
– phuclv
23 hours ago
@phuclv{1,}
is the BRE equivalent of ERE+
. Somesed
implementations support+
as an extension or a-E
or-r
option to enable EREs but that's not portable.?
is another non-portable extension though whose standard equivalent is{0,1}
– Stéphane Chazelas
22 hours ago
@phuclv you wouldn't find unquoted 0xab 0o12 0b1011 numbers in a valid JSON file.
– Stéphane Chazelas
22 hours ago
add a comment |
I tried with below method and it worked fine.
I pipelined 2 times tried up to my level to reduce it
Command:
sed 's/[0-9]{1,},?$/"&/g' filename |
sed '/[0-9]{1,}$/s/[0-9]{1,}/&"/g'|
sed '/[0-9]{1,},$/s/,$/"&/g`'
Output:
{
"id":"1",
"customer":"user",
"plate":"BMT-216-A",
"country":"GB",
"amount":"1000",
"pndNumber":"20000",
"zoneNumber":"4"
}
@Kusalananda corrected the code
– Praveen Kumar BS
yesterday
why do you use{1,},
? To test whether an element appears one or more times use+
. And this won't work for numbers like -123, 0xab, 0o12, 0b1011, 1e23 or 1.2e3...
– phuclv
23 hours ago
@phuclv{1,}
is the BRE equivalent of ERE+
. Somesed
implementations support+
as an extension or a-E
or-r
option to enable EREs but that's not portable.?
is another non-portable extension though whose standard equivalent is{0,1}
– Stéphane Chazelas
22 hours ago
@phuclv you wouldn't find unquoted 0xab 0o12 0b1011 numbers in a valid JSON file.
– Stéphane Chazelas
22 hours ago
add a comment |
I tried with below method and it worked fine.
I pipelined 2 times tried up to my level to reduce it
Command:
sed 's/[0-9]{1,},?$/"&/g' filename |
sed '/[0-9]{1,}$/s/[0-9]{1,}/&"/g'|
sed '/[0-9]{1,},$/s/,$/"&/g`'
Output:
{
"id":"1",
"customer":"user",
"plate":"BMT-216-A",
"country":"GB",
"amount":"1000",
"pndNumber":"20000",
"zoneNumber":"4"
}
I tried with below method and it worked fine.
I pipelined 2 times tried up to my level to reduce it
Command:
sed 's/[0-9]{1,},?$/"&/g' filename |
sed '/[0-9]{1,}$/s/[0-9]{1,}/&"/g'|
sed '/[0-9]{1,},$/s/,$/"&/g`'
Output:
{
"id":"1",
"customer":"user",
"plate":"BMT-216-A",
"country":"GB",
"amount":"1000",
"pndNumber":"20000",
"zoneNumber":"4"
}
edited yesterday
Kusalananda
134k17255418
134k17255418
answered yesterday
Praveen Kumar BSPraveen Kumar BS
1,5281310
1,5281310
@Kusalananda corrected the code
– Praveen Kumar BS
yesterday
why do you use{1,},
? To test whether an element appears one or more times use+
. And this won't work for numbers like -123, 0xab, 0o12, 0b1011, 1e23 or 1.2e3...
– phuclv
23 hours ago
@phuclv{1,}
is the BRE equivalent of ERE+
. Somesed
implementations support+
as an extension or a-E
or-r
option to enable EREs but that's not portable.?
is another non-portable extension though whose standard equivalent is{0,1}
– Stéphane Chazelas
22 hours ago
@phuclv you wouldn't find unquoted 0xab 0o12 0b1011 numbers in a valid JSON file.
– Stéphane Chazelas
22 hours ago
add a comment |
@Kusalananda corrected the code
– Praveen Kumar BS
yesterday
why do you use{1,},
? To test whether an element appears one or more times use+
. And this won't work for numbers like -123, 0xab, 0o12, 0b1011, 1e23 or 1.2e3...
– phuclv
23 hours ago
@phuclv{1,}
is the BRE equivalent of ERE+
. Somesed
implementations support+
as an extension or a-E
or-r
option to enable EREs but that's not portable.?
is another non-portable extension though whose standard equivalent is{0,1}
– Stéphane Chazelas
22 hours ago
@phuclv you wouldn't find unquoted 0xab 0o12 0b1011 numbers in a valid JSON file.
– Stéphane Chazelas
22 hours ago
@Kusalananda corrected the code
– Praveen Kumar BS
yesterday
@Kusalananda corrected the code
– Praveen Kumar BS
yesterday
why do you use
{1,},
? To test whether an element appears one or more times use +
. And this won't work for numbers like -123, 0xab, 0o12, 0b1011, 1e23 or 1.2e3...– phuclv
23 hours ago
why do you use
{1,},
? To test whether an element appears one or more times use +
. And this won't work for numbers like -123, 0xab, 0o12, 0b1011, 1e23 or 1.2e3...– phuclv
23 hours ago
@phuclv
{1,}
is the BRE equivalent of ERE +
. Some sed
implementations support +
as an extension or a -E
or -r
option to enable EREs but that's not portable. ?
is another non-portable extension though whose standard equivalent is {0,1}
– Stéphane Chazelas
22 hours ago
@phuclv
{1,}
is the BRE equivalent of ERE +
. Some sed
implementations support +
as an extension or a -E
or -r
option to enable EREs but that's not portable. ?
is another non-portable extension though whose standard equivalent is {0,1}
– Stéphane Chazelas
22 hours ago
@phuclv you wouldn't find unquoted 0xab 0o12 0b1011 numbers in a valid JSON file.
– Stéphane Chazelas
22 hours ago
@phuclv you wouldn't find unquoted 0xab 0o12 0b1011 numbers in a valid JSON file.
– Stéphane Chazelas
22 hours ago
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%2f504444%2fwrap-all-numerics-in-json-with-quotes%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