Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

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

robertgeen
Tera Guru

Edwio,

I think what you need to do is use the following to do your json:

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/convertto-json?view=...

If you do something like this:

#'additional_info'="$($object | ConvertTo-Json)"

$object would contain all the json content it should convert it properly since apparently it doesn't take it in Powershell as JSON it takes it as an object. I think this should fix it if you put all your content into a variable and then use ConvertTo-Json on it it should fix things for you. Let me know if this works (I'm still working on getting a copy of the script that was used). Thanks.

edwio
Mega Contributor

can you please elaborate on your answer? i'm not sure that i understand it,

Because i'm already converting $body to JSON string:

 

$bodyJson = $body | ConvertTo-Json

 

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.

I don't see what was wrong with your JSON structure. I've got the same issue and completely stumped why it's ending up as [object object]

So...what did you change to make it work? 

I found your thread over in StackOverflow as well with a useful solution.