I have the below JSON from the curl output and I need to retrieve the IP address from it. I tried the below jq query, but I am getting the below error. I tried several other ways of doing it, but no luck
curl -sH "X-Requested-By: ambari" -u admin:admin -i http://${AMBARI_IP}:8080/api/v1/hosts?fields=Hosts/host_name,Hosts/ip | jq '.[] | {.items.Hosts.ip}'
jq: error: syntax error, unexpected FIELD (Unix shell quoting issues?) at <top-level>, line 1:
.[] | {.items.Hosts.ip}
jq: 1 compile error
(23) Failed writing body
below is the output of curl
HTTP/1.1 200 OK
Date: Fri, 02 Jul 2021 21:04:27 GMT
X-Frame-Options: DENY
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff
Cache-Control: no-store
Pragma: no-cache
Set-Cookie: AMBARISESSIONID=123344.node0;Path=/;HttpOnly
Expires: Thu, 01 Jan 1970 00:00:00 GMT
User: admin
Content-Type: text/plain;charset=utf-8
X-Content-Type-Options: nosniff
Vary: Accept-Encoding, User-Agent
Transfer-Encoding: chunked
{
"href" : "http://10.0.0.33:8080/api/v1/hosts?fields=Hosts/host_name,Hosts/ip",
"items" : [
{
"href" : "http://10.0.0.33:8080/api/v1/hosts/sil.dev.test.com",
"Hosts" : {
"host_name" : "test123.sil.dev.test.com",
"ip" : "10.135.3.119"
}
},
{
"href" : "http://10.0.0.33:8080/api/v1/hosts/test001.sil.dev.test.com",
"Hosts" : {
"cluster_name" : "test_cluster",
"host_name" : "test001.sil.dev.test.com",
"ip" : "10.0.0.33"
}
},
{
"href" : "http://10.0.0.33:8080/api/v1/hosts/test002.sil.dev.test.com",
"Hosts" : {
"cluster_name" : "test_cluster",
"host_name" : "test002.sil.dev.test.com",
"ip" : "10.0.0.34"
}
},
{
"href" : "http://10.0.0.33:8080/api/v1/hosts/test003.sil.dev.test.com",
"Hosts" : {
"cluster_name" : "test_cluster",
"host_name" : "test003.sil.dev.test.com",
"ip" : "10.0.0.35"
}
},
}
Try jq '.items[].Hosts.ip'. This grabs the .items key from the outer object, iterates the items array, then pulls the value from the path .Hosts.ip from each object.
PS > cat a.json
{
"href" : "http://10.0.0.33:8080/api/v1/hosts?fields=Hosts/host_name,Hosts/ip",
"items" : [
{
"href" : "http://10.0.0.33:8080/api/v1/hosts/sil.dev.test.com",
"Hosts" : {
"host_name" : "test123.sil.dev.test.com",
"ip" : "10.135.3.119"
}
},
{
"href" : "http://10.0.0.33:8080/api/v1/hosts/test001.sil.dev.test.com",
"Hosts" : {
"cluster_name" : "test_cluster",
"host_name" : "test001.sil.dev.test.com",
"ip" : "10.0.0.33"
}
},
{
"href" : "http://10.0.0.33:8080/api/v1/hosts/test002.sil.dev.test.com",
"Hosts" : {
"cluster_name" : "test_cluster",
"host_name" : "test002.sil.dev.test.com",
"ip" : "10.0.0.34"
}
},
{
"href" : "http://10.0.0.33:8080/api/v1/hosts/test003.sil.dev.test.com",
"Hosts" : {
"cluster_name" : "test_cluster",
"host_name" : "test003.sil.dev.test.com",
"ip" : "10.0.0.35"
}
}
]
}
PS > cat a.json | jq '.items[].Hosts.ip'
"10.135.3.119"
"10.0.0.33"
"10.0.0.34"
"10.0.0.35"
PS > cat a.json | jq -r '.items[].Hosts.ip'
10.135.3.119
10.0.0.33
10.0.0.34
10.0.0.35
Related
Hello i got the following:
in agent i've add:
UserParameter=sip.register,/usr/bin/tbstatus -D --lvl 0 -x "/sip_registration/domain/user/*" --json
and it gives me pretty json in - zabbix-server
2022-04-28 13:25:24
{
"/sip_registration/domain:SIP/user:jtlc31337/contact:1_jtlc31337_95.213.198.99_5060_1" : {
"binding_state" : "Active",
"state_time_left_sec" : 59,
"contact_nap" : "JT_4033_TIP_LIVECALL",
"expires_from_ua" : 120,
"expires_to_registrar" : 3600,
"expires_from_registrar" : 900,
"expires_to_ua" : 59,
"creation_time" : "2022/04/27 10:54:21",
"last_registration_time" : "2022/04/28 13:24:13",
"contact_remap" : "jtlc31337",
"packet_source_struct" : {
"source_ip" : "95.213.198.99",
"source_port" : 5060,
"destination_ip" : "8.7.6.2",
"destination_port" : 5060,
"transport" : "UDP"
}
}
}
I would like to have a LLD and template with discovery rule that gives:
"binding_state"
"source_ip"
Any hints how to make a discovery rules?
Thanks
This follows on from Extracting selected properties from a nested JSON object with jq which lets the OP there get rid of a load of unwanted properties from a nested object.
I've got the same problem but instead of an array starting [, just have a stream of JSON objects, each like this:
{
"localHostName" : "rest-2-17ve6",
"port" : "80",
"requestHeaders" : {
"x-forwarded-port" : "443",
"x-forwarded-host" : "dummy.com",
"content-length" : "15959431",
"accept" : "*/*",
"x-forwarded-for" : "10.1.9.11",
"authorization" : "hash is present",
"expect" : "100-continue",
"forwarded" : "for=10.5.9.1;host=dummy.com;proto=https",
"content-type" : "application/json",
"host" : "dummy.com",
"x-forwarded-proto" : "https",
"user-agent" : "curl/7.51.0"
},
"uri" : "/2/data/saveList",
"protocol" : "HTTP/1.1",
"threadName" : "http-nio-8080-exec-10",
"requestBytes" : 15959431,
"applicationDuration" : 44135,
"responseStatus" : "200",
"remoteIpAddress" : "10.1.10.1",
"responseHeaders" : {
"X-XSS-Protection" : "1; mode=block",
"Content-Type" : "application/json;charset=UTF-8",
"X-Content-Type-Options" : "nosniff",
"Cache-Control" : "no-cache, no-store, max-age=0, must-revalidate",
"Date" : "Wed, 20 Jun 2018 15:53:27 GMT",
"Transfer-Encoding" : "chunked",
"Vary" : "Accept-Encoding",
"X-Frame-Options" : "DENY",
"Expires" : "0",
"Pragma" : "no-cache"
},
"isoDateTime" : "2018-06-20T15:52:42.466913985Z",
"method" : "POST",
"username" : "rd7y1",
"localIpAddress" : "10.129.9.238",
"responseBytes" : 2,
"requestContentExcerpt" : "blah",
"totalDuration" : 44869,
"responseContentExcerpt" : " [] "
}
I want to filter the stream on the command line so I only get:
{
"isoDateTime" : "2018-06-20T15:52:42.466913985Z",
"method" : "POST",
"username" : "rd7y1",
"requestHeaders.user-agent" : "Rcurl"
}
I tried cat /logs/json.log | jq -cC 'map(requestHeaders|={user-agent})' but I'm getting a syntax error.
Since jq is stream-oriented, you would just use select(...) rather than map(select(...))
It looks like you intend to use .requestHeaders."user-agent" in the criterion for selection.
It's generally recommended to avoid using cat when possible.
According to your stated requirements, you should drop the -c command-line option.
Since "Rcurl" does not appear in your sample input, I'll use the string that does appear.
So in your case, you'd end up with something like:
< /logs/json.log jq '
select(.requestHeaders."user-agent" == "curl/7.51.0")
| {isoDateTime, method, username,
"requestHeaders.user-agent": .requestHeaders."user-agent"}'
I have indexed a json file in Mongodb in collection "clicklog" using a shell command. Below is the result of my shell command:
db.clicklogs.find().pretty()
Output:
{
"_id" : ObjectId("58fe78dcfbe21fa7896552e8"),
"preview" : false,
"offset" : 0,
"result" : {
"search_term" : "484797",
"request_time" : "Sat Apr 01 23:58:49 -0400 2017",
"request_ip" : "127.0.0.1",
"stats_type" : "clickstats",
"upi" : "66024330304",
"unit" : "CITCS",
"job_title" : "IT Engineer",
"vpu" : "ICR",
"organization" : "73",
"location" : "MH",
"city" : "San Diego",
"country" : "USA",
"title" : "TOM",
"tab_name" : "People-Tab",
"page_name" : "PEOPLE",
"result_number" : "1",
"page_num" : "0",
"session_id" : "14e88b44576ad4fdc035bc41529762ad1",
"total_results" : "1",
"_raw":"request_time=Sat Apr 01 23:58:49 -0400 2017,request_ip=127.0.0.1,application=Search,stats_type=clickstats,upi=660243301304,unit=CITCS,job_title=IT Assistant, Client Services,vpu=ICR,location=DHAKA, BANGLADESH (IFC),organization=73,city=Dhaka,country=BANGLADESH,city_code=,search_term=484797,title= Tom,url=http://isearch.worldbank.org/skillfinder/ppl_profile_new/000484797,tab_name=People-Tab,page_name=PEOPLE,result_number=1,page_num=0,filter=qterm=484797,total_results=1,app_environment=production,log_version=1.0,session_id=4e88b44576ad4fdc035bc41529762ad1",
"_time":"2017-04-01T23:58:49.000-0400"
}
}
{"_id" : ObjectId("58fe78dcfbe21fa7896552e9"),
"preview" : false,
"offset" : 0,
"result" : {
"search_term" : "demo",
"request_time" : "Sat Apr 01 23:58:49 -0400 2017",
"request_ip" : "127.0.0.1",
....
"time":"2017-04-01T23:58:49.000-0400"
}
}
For every json document, I would like to get only the few field(id,searchterm,upi,page_name,sessionid, url(which is under _raw)). Is it possible to do it using mongo shell commands and store the result document in a new collection? Any help is appreciated.
You can try below aggregation in 3.4 version.
The query uses $split operator couple of times to reach to url value. Rest is standard projection fields.
$out stage to write the results into new collection.
db.getCollection('clicklogs').aggregate([{
$project: {
searchterm: "$result.searchterm",
upi: "$result.upi",
page_name: "$result.page_name",
session_id: "$result.session_id",
url: {
$let: {
vars: {
obj: {
$arrayElemAt: [{
$split: ["$result._raw", ',']
}, 1]
}
},
in: {
$arrayElemAt: [{
$split: ["$$obj", '=']
}, 1]
}
}
}
}
},
{
$out: "clicklogs_temp"
}
])
Can someone please help me with the Json Request to upload multiple documents in a envelope. I have pasted the request below.
The error that I get is
API call failed, status returned was: 400
Error description:
{ "errorCode": "NO_DOCUMENT_RECEIVED", "message": "The document element did not contain the encoded document, or there is a problem with the encoding. Bytes for document corresponding to documentId 1 not found in request. 'documentId=' possibly missing from Content-Disposition header."}
--BOUNDARY
Content-Type: application/json
Content-Disposition: form-data
{
"emailSubject" : "This is email subject",
"emailBlurb" : "This is email body",
"recipients" : {
"signers" : [ {
"routingOrder" : "1",
"name" : "name",
"email" : "valid email address",
"recipientId" : "1",
"tabs" : {
"signHereTabs" : [ {
"anchorTab" : {
"anchorString" : "SignHere",
"anchorXOffset" : "1",
"anchorYOffset" : "1",
"anchorIgnoreIfNotPresent" : "true",
"anchorUnits" : "inches"
}
} ]
}
} ]
},
"documents" : [ {
"name" : "document.pdf",
"documentId" : "1"
}, {
"name" : "document2.pdf",
"documentId" : "2"
} ],
"status" : "sent"
}
--BOUNDARY
Content-Disposition: form-data
Content-Type: multipart/mixed; boundary=BBB
--BBB
Content-Type: application/pdf
Content-Disposition: file; filename="document.pdf"; documentid="1"
[B#47df4d31
--BBB
Content-Type: application/pdf
Content-Disposition: file; filename="document2.pdf"; documentid="2"
[B#1544f0d4
--BBB--
--BOUNDARY--
I have an express app, which has a post method(the post is a json type):
server.js(simplified version):
app.post('/listener/v1/event/', function(req, res) {
.
.
var event = req.body;
var validator = require("./validator");
validator.validate(event);
}
validator.js contains the validation for the json:
var jsonschemavalidate = require("json-schema");
var basicSchema = require('fs').readFileSync('./schema.json', 'utf8');
exports.validate = function (event) {
console.log(jsonschemavalidate.validate(event, basicSchema).errors);
}
The schema.json:
{
name : "test",
type : 'object',
properties : {
event_id : { type : 'string' },
timestamp : { type : 'string' }
}
}
For the input I use curl:
curl -i -X POST -H 'Content-Type: application/json' -d '{"event_id": "NedaleGassss", "timestamp": "a2009321"}' http://localhost:3000/listener/v1/event/
The output is as follows:
[ { property: '',
message: 'Invalid schema/property definition {\n name : "test",\n type : "object",\n additionalProperties : false,\n properties :\n {\n event_id : { type : "string" },\n timestamp \t: { type : "string" }\n }\n}' } ]
Your schema is invalid, as the error says. The schema should also be valid JSON,
so properties and strings should be double quoted:
{
"name" : "test",
"type" : "object",
"properties" : {
"event_id" : { "type" : "string" },
"timestamp" : { "type" : "string" }
}
}
This should do the trick, (unless you figured it out already in the past year)
And also:
var basicSchema = require('fs').readFileSync('./schema.json', 'utf8');
could probably be replaced by:
var basicSchema = require('./schema');