使用shell脚本中的jq过滤器从stringify对象中获取json对象

时间:2015-09-17 11:32:14

标签: json linux shell jq

我正在拧我的shell脚本,因为我是新手。我的疑问是 我有像json对象

{
   "logo": {"name":"logo.png","type":"image\/jpeg","tmp_name":"C:\\xampp\\tmp\\php8B97.tmp","error":0,"size":110290},
   "template":"template1",
   "firstname":"a",
   "lastname":"a",
   "username":"a",
   "password":"aa",
   "email":"a",
   "categoriesListArr":"{\"Women\":[\"All footwear\",\"All footwear\",\"All Clothing\",\"All Clothing\",\"All Watches\",\"All Watches\",\"All Sunglasses\",\"All Sunglasses\"],\"Men\":[\"All Mens Accessories\",\"All Bags,Belts And wallets\",\"All Fragrances\",\"All Grooming and wellness\"]}",
   "aboutUs":"aa",
   "contactUs":"78787878878787",
   "deliveryInfo":"aa",
   "privacyPolicy":"aa",
   "t&d":"aa"
}

我使用jq过滤器提取了categoriesListArr,如下所示:

categories=`cat detail.json| jq '.categoriesListArr'`

detail.json是文件的名称。 现在类别是stringify对象...我需要解析它并将其转换为json对象。

"{\"Women\":[\"All footwear\",\"All footwear\",\"All Clothing\",\"All Clothing\",\"All Watches\",\"All Watches\",\"All Sunglasses\",\"All Sunglasses\"],\"Men\":[\"All Mens Accessories\",\"All Bags,Belts And wallets\",\"All Fragrances\",\"All Grooming and wellness\"]}"

我如何使用jq过滤器转换它

1 个答案:

答案 0 :(得分:5)

fromjson 是你的朋友:

$ jq '.categoriesListArr | fromjson' detail.json

或者,如果您想保留原始结构:

$ jq '.categoriesListArr |= fromjson' detail.json