em_event table additional_Info field is returning [object Object]

edwio
Mega Contributor

We are using Rest API via PowerShell (Invoke-RestMethod),
In order to insert records in the event [em_event] table with a single call, using the web service API.

We successfully inserting events to the em_event table,
But the only problem is with the additional_info field.

For some reason,
The JSON structure of my PowerShell script,
Is causing the output of additional_info, to return as an Object and Not as JSON string.

And as a result,
Not showing the values in additional_info properly, but instead as [object Object]:

find_real_file.png

This is the JSON structure in my PowerShell script:

# Specify request body
$body = @"
{	"records":	
[
     {
     "source":"MyClass",
     "event_class":"$AtargetResourceType",
     "resource":"$AtargetResourceType",
     "node":"$AtargetResourceName",
     "metric_name":"$Aname",
     "type":"$AsignalType",
     "severity":"$Aseverity",
     "message_key":"$Aid",
	 "u_mc_object":"$AtargetResource",
     "description":"$Adescription",
     "additional_info":"{  
							'u_mc_object_class':'$AsourceCreatedId',
							'u_mc_parameter':'$AmetricName',
							'u_mc_parameter_value':'$AmetricValue'
     					}"
      }     
   ]
}
"@
1 ACCEPTED SOLUTION

edwio
Mega Contributor

Turns out,
My Structure was effecting the only the additional_info section in my JSON.

Fixed,
Currently working as it should be:

find_real_file.png

Thanks for the help.

View solution in original post

14 REPLIES 14

Alberto Consonn
ServiceNow Employee
ServiceNow Employee

Hi,

use the solution provided to the thread below:

The issue here is that the event engine is expecting additional_info to be a JSON string, but it's being sent as an object instead.

https://community.servicenow.com/community?id=community_question&sys_id=12a24f21dbd8dbc01dcaf3231f96...

If I have answered your question, please mark my response as correct and/or helpful.

Thank you very much

Cheers
Alberto

I already wrote the cause of my problem,

And also reviewed the link you wrote, 
but I'm using PowerShell Not Perl.

SteveMac1
Mega Guru

The problem is the single quotes. Single quotes are not valid in JSON. Replace them with double quotes and you'll be good to go. I believe you can embed double quotes in a string by escaping them with a backtick character (`). 

edwio
Mega Contributor

@SteveMac, at first i thought that this could be the reason. but event with double quotes i"m still getting the same resault: [object Object].

This is the full ServiceNow Section in my Script:

# Eg. User name="admin", Password="admin" for this code sample.
$user = "MyUser"
$pass = "MyPassword"

# Build auth header
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user, $pass)))

# Set proper headers for SNOW
$SNOWheaders = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$SNOWheaders.Add('Authorization',('Basic {0}' -f $base64AuthInfo))
$SNOWheaders.Add('Accept','application/json')
$SNOWheaders.Add('Content-Type','application/json')

# Specify endpoint uri

# SNOW URI
$uri = "https://servicenow-myinstance/api/global/em/jsonv2"

# Specify HTTP method
$method = "Post"

# Specify request body
$body = @"
{	"records":	
[
     {
     "source":"MyClass",
     "event_class":"$AtargetResourceType",
     "resource":"$AtargetResourceType",
     "node":"$AtargetResourceName",
     "metric_name":"$Aname",
     "type":"$AsignalType",
     "severity":"$Aseverity",
     "message_key":"$Aid",
	 "u_mc_object":"$AtargetResource",
     "description":"$Adescription",
     "additional_info":"{  
							"u_mc_object_class":"$AsourceCreatedId",
							"u_mc_parameter":"$AmetricName",
							"u_mc_parameter_value":"$AmetricValue"
     					}"
      }     
   ]
}
"@

$bodyJson = $body | ConvertTo-Json

# Send HTTP reques
$response = Invoke-RestMethod -Headers $SNOWheaders -Method $method -Uri $uri -Body $bodyJson -ContentType "application/json" -UseBasicParsing