Forcing miller to read data as string in conversion to JSON

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

Asked By: Marcos


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)'
Answered By: aborruso
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.