How to convert data format in JSON using SED

How to convert data format in JSON using SED



I have a huge NDJSON file wherein one field is "createDate":"01/02/2018". It is in dd/mm/yyyy format and I need to convert it into yyyy-mm-dd format.


"createDate":"01/02/2018"


dd/mm/yyyy


yyyy-mm-dd



I can do this using sed on a small input using the below command:


sed


echo 28/02/2018 | sed 's,([0-9][0-9])/([0-1][0-9])/([1-2][0-9][0-9][0-9]),3-2-1,'



However, I'm unable to find a solution wherein I have to do this in a JSON file where this value is under a key with name "createDate".


"createDate"



A sample JSON object looks like this:



"pushNotificationEnabled": "true",
"createDate": "11/08/2018",
"email": null,
"photoUrl": null



Any help is greatly appreciated.




2 Answers
2



Your command works on your sample JSON object! You might want to restrict its action to the createDate field:


createDate


sed '/"createDate":/s,([0-9][0-9])/([0-1][0-9])/([1-2][0-9][0-9][0-9]),3-2-1,' input.json



This will only affect lines containing the "createDate": tag:


"createDate":


==> input.json <==

"pushNotificationEnabled": "true",
"createDate": "11/08/2018",
"modifyDate": "31/08/2018",
"email": null,
"photoUrl": null

$ sed '/"createDate":/s,([0-9][0-9])/([0-1][0-9])/([1-2][0-9][0-9][0-9]),3-2-1,' input.json

"pushNotificationEnabled": "true",
"createDate": "2018-08-11",
"modifyDate": "31/08/2018",
"email": null,
"photoUrl": null






Thanks a ton for the solution!

– Sains
Sep 9 '18 at 23:18



Assuming each object in the NDJSON file will fit comfortably in memory, an invocation of jq along the following lines should do the job, regardless of how large the file itself is, because jq will (by default) only read in one JSON entity at a time:


jq '.createDate |=
sub("^(?<m>[0-9]*)/(?<d>[0-9]*)/(?<y>[0-9]*)"; "(.y)-(.m)-(.d)")' input.json



Although jq might be slightly less efficient than sed for the task, it does understand JSON.



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

How do I collapse sections of code in Visual Studio Code for Windows?

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ