2

I have a JSON file with several uid fields that are numeric but I need to convert them to string. These uid fields are nested in different places in the file.

e.g.   "uid": 891602734537070110  => "uid": "891602734537070110"

I tried this command:

jq '(.. | .uid?) |= (tostring)'

which somewhat worked but it added a "uid" field (e.g. "uid": "null") to any dictionaries that didn't have one to begin with.

What is the correct way of doing this?

1
  • 2
    Just beware that those are very large numbers. It will have double precision so the values may actually end up different as a string. github.com/stedolan/jq/issues/1741 Commented Aug 25, 2019 at 19:34

1 Answer 1

2

The correct way of doing this is walk:

walk(
  if type == "object" and has("uid")
  then .uid |= tostring
  else . end
)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.