使用jq将JSON文件中键值的字符串分配给该文件中的其他键

utugiqy6  于 2022-12-15  发布在  其他
关注(0)|答案(1)|浏览(103)

目标是在JSON文件中使用jq将hrefFull的一部分分配给hrefSimplehrefSubsite。可能有更好的方法来实现这一点,但我已经通过寻找一种解决方案来实现这一点,该解决方案删除键值中字符串articles之前的所有内容,但保留该字符串。多个对象(如下面的示例对象)包含在一个JSON文件中,该文件的格式为:开头为[,结尾为]
预期结果:

  • hrefFull不会更改。从hrefFull提取的字符串将应用于hrefSimplehrefSubsite
  • hrefSimplearticles之后的所有内容(包括articles)。如果articles不在字符串中,则hrefSimple是最后一个/之后的字符串。请参阅示例对象7。
  • hrefSubsite是介于https://docs.mysite.com//articles...之间的字符串。

示例结果-对象1:

{
  "hrefFull": "https://docs.mysite.com/product-a/articles/page-a.html",
  "hrefSimple": "articles/page-a.html",
  "hrefSubsite": "product-a"
}

示例结果-对象2:

{
  "hrefFull": "https://docs.mysite.com/product-b/articles/guide-b/page-b.html",
  "hrefSimple": "articles/guide-b/page-b.html",
  "hrefSubsite": "product-b"
}

示例结果-对象3:

{
  "hrefFull": "https://docs.mysite.com/product-c/articles/guide-c/section-c/page-c.html",
  "hrefSimple": "articles/guide-c/section-c/page-c.html",
  "hrefSubsite": "product-c"
}

示例结果-对象4:

{
  "hrefFull": "https://docs.mysite.com/product-d/sub-product-d/articles/page-d.html",
  "hrefSimple": "articles/page-d.html",
  "hrefSubsite": "product-d/sub-product-d"
}

示例结果-对象5:

{
  "hrefFull": "https://docs.mysite.com/product-e/sub-product-e/articles/guide-e/page-e.html",
  "hrefSimple": "articles/guide-e/page-e.html",
  "hrefSubsite": "product-e/sub-product-e"
}

示例结果-对象6:

{
  "hrefFull": "https://docs.mysite.com/product-f/sub-product-f/articles/guide-f/section-f/page-f.html",
  "hrefSimple": "articles/guide-f/section-f/page-f.html",
  "hrefSubsite": "product-f/sub-product-f"
}

示例结果-对象7:

{
  "hrefFull": "https://docs.mysite.com/product-g/index.html",
  "hrefSimple": "index.html",
  "hrefSubsite": "product-g"
}

尝试失败(在Bash脚本中):

siteUrl="docs.mysite.com"
jq '
(.hrefSimple = .hrefFull)
| .hrefSimple |= (gsub("https://\($siteUrl)/.*?/"; ""))
| (.hrefSubsite = .hrefFull)
| .hrefSubsite |= (gsub("https://\($siteUrl)/"; ""))
' file-1.json > file-2.json

该脚本生成准确和不准确的结果。
准确结果:

  • 对象1
  • 对象2
  • 对象3
  • 对象7

不准确结果:

  • 对象四:
  • hrefSimple错误地为sub-product-d/articles/page-d.html,而不是articles/page-d.html
  • hrefSubsite错误地为sub-product-d,而不是product-d/sub-product-d
  • 对象五:
  • hrefSimple错误地为sub-product-e/articles/guide-e/page-e.html,而不是articles/guide-e/page-e.html
  • hrefSubsite错误地为sub-product-e,而不是product-e/sub-product-e
  • 目标六:
  • hrefSimple错误地为sub-product-f/articles/guide-f/section-f/page-f.html,而不是articles/guide-f/section-f/page-f.html
  • hrefSubsite错误地为sub-product-f,而不是product-f/sub-product-f

其他不成功的尝试(如果有帮助,我可以提供准确的结果):

  • .hrefSimple |= (gsub("https://\($siteUrl)/.*?/"; "")).hrefSubsite |= (gsub("https://\($siteUrl)/"; ""))形式的articles的各种迭代
  • .hrefSimple |= split("articles")[0]的各种迭代(也在.hrefSubsite内)

如果有必要的话,hrefFull来自文档网站的Azure App Insights页面视图导出。导出的数据用于分析报表。我正在创建hrefSimple以联接两个表,并希望在hrefSubsite上筛选。hrefFull中的路径是在使用DocFx静态站点生成器生成网站并部署到Azure Blob时生成的。

g6ll5ycj

g6ll5ycj1#

我将capture与正则表达式一起使用:

. + (.hrefFull | capture(
  "^https://docs.mysite.com/(?<hrefSubsite>.*?)/(?<hrefSimple>articles.*|[^/]*)$"
))
{
  "hrefFull": "https://docs.mysite.com/product-a/articles/page-a.html",
  "hrefSubsite": "product-a",
  "hrefSimple": "articles/page-a.html"
}
{
  "hrefFull": "https://docs.mysite.com/product-b/articles/guide-b/page-b.html",
  "hrefSubsite": "product-b",
  "hrefSimple": "articles/guide-b/page-b.html"
}
{
  "hrefFull": "https://docs.mysite.com/product-c/articles/guide-c/section-c/page-c.html",
  "hrefSubsite": "product-c",
  "hrefSimple": "articles/guide-c/section-c/page-c.html"
}
{
  "hrefFull": "https://docs.mysite.com/product-d/sub-product-d/articles/page-d.html",
  "hrefSubsite": "product-d/sub-product-d",
  "hrefSimple": "articles/page-d.html"
}
{
  "hrefFull": "https://docs.mysite.com/product-e/sub-product-e/articles/guide-e/page-e.html",
  "hrefSubsite": "product-e/sub-product-e",
  "hrefSimple": "articles/guide-e/page-e.html"
}
{
  "hrefFull": "https://docs.mysite.com/product-f/sub-product-f/articles/guide-f/section-f/page-f.html",
  "hrefSubsite": "product-f/sub-product-f",
  "hrefSimple": "articles/guide-f/section-f/page-f.html"
}
{
  "hrefFull": "https://docs.mysite.com/product-g/index.html",
  "hrefSubsite": "product-g",
  "hrefSimple": "index.html"
}

Demo
如果输入对象位于数组中,请将此筛选器 Package 为map(…)

相关问题