JSON用变量

时间:2018-04-25 10:51:04

标签: json regex file variables replace

我有一个JSON文件,其中有Dateobjects。我想用Formated Date Strings替换它们。有人能给我一个暗示如何以一种简单的方式实现这一目标吗?

在JSON文件中有以下行:

  

“insertedAt”:{“$ date”:“2018-01-31T11:05:39.447Z”},

我想用以下内容替换它们:

  

“insertedAt”:“2018-01-31 11:05:39.447000”

这些文件非常大,所以我想避免将它们加载到JSON-Objects中,并使用正则表达式替换或类似方法来解决它。不幸的是,我对它并不是很有经验。

是否可以用自身的功能输出替换某些东西? 我可以选择要用正则表达式替换的部分:

"insertedAt":{".*"}

我现在可以用*的功能替换它吗?在Pseudocode中它就像:

replace("\"insertedAt\":{\".*\"}").with("\"insertedAt\":" + format(*))

还有其他想法吗?

1 个答案:

答案 0 :(得分:0)

如果您可以在regex engine中使用(?<=insertedAT\": )\{ \"\$date\": (\"[^\"]*\") \}, ,那么您可以尝试将其用于匹配目标字符串

group 1

并将其替换为捕获\1$1lookbehind)。 Demo

或者,如果您无法在regex engine中使用regex,那么您可以尝试(?!insertedAT\": )\{ \"\$date\": (\"[^\"]*\") \},

jq

Demo

相关问题