0

In the following MWE

echo x="1e2" | mlr --ojson cat

my intention is for miller to generate a one-element JSON array containing the object

{"x": "1e2"}

The object actually returned (within the array) is instead

{"x": 1e2}

where the value is taken as a number, I guess that as a consequence of its parsing. How can I tell miller to generate the JSON object with a string for its value rather than a number? (The rationale underlying the quotation marks around '1e2' in the MWE is precisely to highlight this intention.)

Marcos
  • 103
  • 3
  • 1
    In `echo x="1e2"`, do you think the output includes the quotes? – muru Jul 31 '23 at 05:50
  • @muru: I get your point. I was only highlighting my intention to have as value a string rather than a number. Escaping the quotation marks would lead to a string value, but one including the latter (escaped) marks. – Marcos Jul 31 '23 at 17:23

1 Answers1

5

In Miller 6 you can use -S flag, which forces Miller to treat the data as strings:

echo x="1e2" | mlr --ojson -S cat

to get

[
{
  "x": "1e2"
}
]

Or you can cast it using string function

echo x="1e2" | mlr --ojson put '$x=string($x)'
Kusalananda
  • 320,670
  • 36
  • 633
  • 936
aborruso
  • 2,618
  • 10
  • 26