如何将双重Qoutes更改为JSON中儿童对象的单个Qoutes?

发布于 2025-02-09 19:50:05 字数 461 浏览 1 评论 0原文

我有一个JSON文本文件,需要将其导入到数据库中。除了一分钟的差异之外,我已经准备好了,即我需要将双重Qoutes更改为所有嵌套级别儿童的单Qoutes/使用JS的JSON值。

例如,

{"foo":"bar","ping":{"pong":"ping-pong","n":[{"a":1},{"b":2}]},"alpha":"beta","hello":"there!","you":"2"}

预期输出为:

{"foo":"bar","ping":{'pong':'ping-pong','n':[{'a':1},{'b':2}]}, "alpha":"beta","hello":"there!","you":"2"}

I have a JSON text files and I need to import it into a database. I have prepared it well except for a minute difference ie I need to change the double qoutes to single qoutes for all nested level children/values of JSON using jq.

For example,

{"foo":"bar","ping":{"pong":"ping-pong","n":[{"a":1},{"b":2}]},"alpha":"beta","hello":"there!","you":"2"}

and expected output is:

{"foo":"bar","ping":{'pong':'ping-pong','n':[{'a':1},{'b':2}]}, "alpha":"beta","hello":"there!","you":"2"}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小霸王臭丫头 2025-02-16 19:50:05

要将任何JSON对象变成JSON编码的字符串使用JQ的tojson@json functions:

jq '.ping |= tojson' example.json

demo

jq '.ping |= @json' example.json

demo

{
  "foo": "bar",
  "ping": "{\"pong\":\"ping-pong\",\"n\":[{\"a\":1},{\"b\":2}]}",
  "alpha": "beta",
  "hello": "there!",
  "you": "2"
}

To turn any JSON object into a JSON-encoded string use jq's tojson or @json functions:

jq '.ping |= tojson' example.json

Demo

jq '.ping |= @json' example.json

Demo

Both produce

{
  "foo": "bar",
  "ping": "{\"pong\":\"ping-pong\",\"n\":[{\"a\":1},{\"b\":2}]}",
  "alpha": "beta",
  "hello": "there!",
  "you": "2"
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文