Surround Field from JSON with quotes

0

I want to change a field in json from number to string format.

I create the json output from a dynamical created text file like this:

LastUpdate=2019-12-01T13:26:46.388817319+01:00
State=NOT OK
TempA=26.299999
TempB=-0.100000
PumpState=0
Vault=100.000000
Mode=H+WW
ErrState=0

then I use jo to convert this output to json

cat /tmp/file | jo -p 
{
   "LastUpdate": "2019-12-01T13:26:46.388817319+01:00",
   "State": "NOT OK",
   "TempA": 26.299999,
   "TempB": -0.1,
   "PumpState": 0,
   "Vault": 100,
   "Mode": "H+WW",
   "ErrState": 0
}

Problem is, that I need the value ErrState to be in string format.

It looks like jo cant do it on piped input. So I was thinking it is maybe possible with sed or jq? Or any other tool?

Problem could be, that Output from ErrState can change to text, than I will have two times quotes on a simple search & replace.

Remember I want to keep the full output - only change ErrState to string.

Thomas

Posted 2019-12-01T12:36:44.000

Reputation: 126

Answers

1

With jq you can use:

.ErrState = (.ErrState | tostring)

user1686

Posted 2019-12-01T12:36:44.000

Reputation: 283 655

hmm.. not bad - but it change 0 to null – Thomas – 2019-12-01T13:45:29.900

ok, only on the only version from jq play - in the script that works THANKS :) – Thomas – 2019-12-01T13:48:13.663

edit: online version from jq play ;) – Thomas – 2019-12-01T13:55:19.543