Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

grep - How to get name value in the below json file

[
  {
    "_class": "hudson.model.FreeStyleProject",
    "name": "actual_build",
    "url": "http://tepl.tallycloudinfra.com:8080/job/actual_build/",
    "color": "blue",
    "fullname": "actual_build"
  },
  {
    "_class": "hudson.model.FreeStyleProject",
    "name": "CIS_TEPL_QA_Deploy",
    "url": "http://tepl.tallycloudinfra.com:8080/job/CIS_TEPL_QA_Deploy/",
    "color": "blue",
    "fullname": "CIS_TEPL_QA_Deploy"
  },
  {
    "_class": "hudson.model.FreeStyleProject",
    "name": "cktest",
    "url": "http://tepl.tallycloudinfra.com:8080/job/cktest/",
    "color": "red",
    "fullname": "cktest"
  },
  {
    "_class": "hudson.model.FreeStyleProject",
    "name": "Create_New_DB_Structure",
    "url": "http://tepl.tallycloudinfra.com:8080/job/Create_New_DB_Structure/",
    "color": "blue",
    "fullname": "Create_New_DB_Structure"
  }
]

I tried below jq command i'm getting this error.

Error:

jq -r '._class[].name' f1.json
jq: error (at f1.json:30): Cannot index array with string "_class"

jq -r '._class.name' f1.json
jq: error (at f1.json:30): Cannot index array with string "_class"
question from:https://stackoverflow.com/questions/65950072/how-to-get-name-value-in-the-below-json-file

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

With your input,

jq '.[].name' f1.json

yields

"actual_build"
"CIS_TEPL_QA_Deploy"
"cktest"
"Create_New_DB_Structure"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...