user10573594
user10573594

Reputation: 141

jq filter out specific pattern

I would like to retrieve the pointURL of the following JSON via jq. In other words, how can I get the pointURL": "http://10.1.1.64:21138" ?

{
"id": "b8e07db15f30",
"configuration": "{\r\n  \"encd\": [\r\n    {\r\n      \"name\": \"hello\",\r\n      \"raiseOnError\": true,\r\n      \"type\": \"version3\",\r\n      \"encdpecific\": {\r\n        \"apiURL\": \"http://172.29.32.32/new/rest\",\r\n        \"apiURLB\": \"http://172.29.32.32/new/rest\",\r\n        \"IDname\": 92,\r\n        \"startno\": 5000,\r\n        \"support\": true\r\n      },\r\n      \"ServerURL\": {\r\n        \"type\": \"ServerNEW\",\r\n        \"ServerURLSpecific\": {\r\n          \"primaryPoint\": {\r\n            \"pointURL\": \"http://10.1.1.64:21138/{{id:hello}}/hello.txt\",\r\n            \"ServerNEWAPI\": \"https://api.example.com/hello/api1\",\r\n            \"ServerNEWAPIKey\": \"6d0a0a8d-6fb9-4f14-a72e\",\r\n            \"filePath\": \"/\",\r\n            \"create\": true,\r\n            \"liveName\": \"hello.txt\",\r\n            \"livedlived\": \"hello.txt\"\r\n          },\r\n          \"secondaryPoint\": {\r\n            \"pointURL\": \"http://10.1.1.65:21139/{{id:hello}}/hello.txt\",\r\n            \"ServerNEWAPI\": \"https://api.example.com/hello/api2\",\r\n            \"ServerNEWAPIKey\": \"6d0a0a8d-6fb9-4f14-a72e\",\r\n            \"filePath\": \"/\",\r\n            \"create\": true,\r\n            \"liveName\": \"hello.txt\",\r\n            \"livedlived\": \"hello.txt\"\r\n          }\r\n        }\r\n      },\r\n      \"outputUrls\": [\r\n        {\r\n          \"protocol\": \"newtype\",\r\n          \"newthroughUrl\": \"https://new.example.com/{{id:hello}}/hello.txt/.txt\",\r\n          \"newthroughUrl\": \"\",\r\n          \"originPrimaryUrl\": \"http://10.1.1.64:21138/{{id:hello}}/hello.txt/.txt\",\r\n          \"liveOriginSecondaryUrl\": \"http://10.1.1.65:21139/{{id:hello}}/hello.txt/.txt\",\r\n          \"throughUrl\": \"https://api.example.com/{{id:hello}}/hello.txt/.txt\"\r\n        },\r\n        {\r\n          \"protocol\": \"VIEW\",\r\n          \"newthroughUrl\": \"https://new.example.com/{{id:hello}}/hello.txt/.txt\",\r\n          \"newthroughUrl\": \"\",\r\n          \"originPrimaryUrl\": \"http://10.1.1.64:21138/{{id:hello}}/hello.txt/.txt\",\r\n          \"liveOriginSecondaryUrl\": \"http://10.1.1.65:21139/{{id:hello}}/hello.txt/.txt\",\r\n          \"throughUrl\": \"https://api.example.com/{{id:hello}}/hello.txt/.txt\"\r\n        },\r\n        {\r\n          \"protocol\": \"otherprot\",\r\n          \"newthroughUrl\": \"https://new.example.com/{{id:hello}}/hello.txt/man\",\r\n          \"newthroughUrl\": \"\",\r\n          \"originPrimaryUrl\": \"http://10.1.1.64:21138/{{id:hello}}/hello.txt/man\",\r\n          \"liveOriginSecondaryUrl\": \"http://10.1.1.65:21139/{{id:hello}}/hello.txt/man\",\r\n          \"throughUrl\": \"https://api.example.com/{{id:hello}}/hello.txt/man\"\r\n        },\r\n        {\r\n          \"protocol\": \"LLL\",\r\n          \"newthroughUrl\": \"https://new.example.com/{{id:hello}}/hello.txt/.fram\",\r\n          \"newthroughUrl\": \"\",\r\n          \"originPrimaryUrl\": \"http://10.1.1.64:21138/{{id:hello}}/hello.txt/.fram\",\r\n          \"liveOriginSecondaryUrl\": \"http://10.1.1.65:21139/{{id:hello}}/hello.txt/.fram\",\r\n          \"throughUrl\": \"https://api.example.com/{{id:hello}}/hello.txt/.fram\"\r\n        }\r\n      ]\r\n    }\r\n  ]\r\n}",
"name": "hello",
"refID": "hello"}

I tried the following, but it doesn't show the required, as it prints the whole configuration:

jq '.[] | {name,configuration}'

thank you.

Upvotes: 0

Views: 307

Answers (2)

Aaron
Aaron

Reputation: 24802

Hard to be sure since there are two pointURL and none have the exact value you asked for, but I think you want the following jq command :

.configuration | fromjson | .encd[0].ServerURL.ServerURLSpecific.primaryPoint.pointURL

Try it here.

If you only need the protocol and hostname parts of the URL you can use scan with a regex that will only match those parts :

.configuration | fromjson
  | .encd[0].ServerURL.ServerURLSpecific.primaryPoint.pointURL
  | scan("^[^:]+://[^/]+")

Try it here

Use with jq -r to avoid the enclosing quotes that are part of a valid JSON string.

Upvotes: 4

GandhiGandhi
GandhiGandhi

Reputation: 1350

I had some trouble running an example because the pasted JSON snippet doesn't seem to work with jq.

Try this though.

jq '.configuration.encd | .[] | .ServerURL.ServerURLSpecific.primaryPoint.pointURL'

Upvotes: 0

Related Questions