PowerShell PSObject change field names before converted to JSON object - powershell

I have a project in PowerShell where I created a custom type using the "Add-Type". I wanted to use classes, but I'm limited to the PowerShell version on the system I have to use.
This object gets converted to JSON and is sent out using a REST API. To avoid some naming issues I ended up pre-pending ""s to the field names. I want to remove those before it is sent to the API. I thought it would be simple to do the rename, and it was, but then I realized the method I used for the rename would potentially cause issues if I renamed everything in the JSON object by removing anything with the "".
Here is the custom type:
Add-Type -typedef #"
public class SearchFilter
{
public int _filterOrder;
public string _property;
public string _direction;
public string _value;
public string[] _values;
public string _operator;
public SearchFilter()
{
_filterOrder = 0;
_property = "";
_direction = "";
_value = "";
_values = new string[0];
_operator = "";
}
}
"#
Using just a string REPLACE method I'll get this:
{
"filterOrder": 0,
"property": "name",
"direction": "ASC",
"value": "value",
"values": [],
"operator": "Contains"
}
instead of this:
{
"filterOrder": 0,
"property": "name",
"direction": "ASC",
"value": "_value_",
"values": [],
"operator": "Contains"
}
Here is the method I'm using:
# Create search filter object
$searchFilter = New-Object -TypeName "SearchFilter"
# Convert search filter to REST API JSON format
$apiPostBody = ConvertTo-Json -InputObject $searchFilter
# Remove all "_"
$apiPostBody = $apiPostBody .Replace("_","")
I have checked the methods available from PSObject, but I'm just not sure of the syntax. If possible I'd like to change the existing object before converting to JSON as opposed to having to create a CLONE or iterate through the JSON value.

Related

powershell convert-tojson not giving optimal results

I have a powershell question, I need to convert this piece of code to Json
$Dimensions = #(
#{
name = 'num_vm'
value = '100'
},
#{
name = 'days'
value = '10'
},
#{
name = 'months'
value = '12'
}
)
For ($i=0;$i -lt $Dimensions.length;$i++)
{
$bodymessage = #{'resourceId' = $resourceUsageId; 'quantity' = "$Dimensions[$i].value"; 'dimension' = "$Dimensions[$i].name"; 'effectiveStartTime' = $lastHourMinusFiveMinutes; 'planId' = 'plan_meter' }
$body += #{
request =
#($bodymessage)} `
|ConvertTo-Json -Depth 5
}
$body
what I'm hoping, is it can dynamically create the JSON output according to the dimension input,
but the result I got is this
{
"request": [
{
"resourceId": null,
"quantity": "System.Collections.Hashtable System.Collections.Hashtable System.Collections.Hashtable[0].value",
"planId": "plan_meter",
"dimension": "System.Collections.Hashtable System.Collections.Hashtable System.Collections.Hashtable[0].name",
"effectiveStartTime": null
}
]
}{
"request": [
{
"resourceId": null,
"quantity": "System.Collections.Hashtable System.Collections.Hashtable System.Collections.Hashtable[1].value",
"planId": "plan_meter",
"dimension": "System.Collections.Hashtable System.Collections.Hashtable System.Collections.Hashtable[1].name",
"effectiveStartTime": null
}
]
}{
"request": [
{
"resourceId": null,
"quantity": "System.Collections.Hashtable System.Collections.Hashtable System.Collections.Hashtable[2].value",
"planId": "plan_meter",
"dimension": "System.Collections.Hashtable System.Collections.Hashtable System.Collections.Hashtable[2].name",
"effectiveStartTime": null
}
]
}
how can i get rid of System.Collections.Hashtable System.Collections.Hashtable System.Collections.Hashtable[0].value?
and displaying with real value?
The main issue is how you're assigning the hashtable element values within a string. Fix it by removing the quotes like so (formatted multiline for readability):
$bodymessage = #{
resourceId = $resourceUsageId;
quantity = $Dimensions[$i].value; # Removed the quotes here
dimension = $Dimensions[$i].name; # Removed the quotes here
effectiveStartTime = $lastHourMinusFiveMinutes;
planId = 'plan_meter';
}
Note: I've also removed the single-quotes ' around the key names since they are not needed in this context. You only need to quote key names if non-alphanumeric characters are part of the key name, and you are assigning the key name as a literal string.
Why did this happen?
This happens because of how variable expansion within strings works. The following string you originally had:
"$Dimensions[$i].value"
will see $Dimensions get expanded (its ToString() function is implicitly called), not $Dimensions[$i].value. This is because variable parsing starts at the dollar-sign $ and stops at the first character which can't normally be part of a variable name. This will be most special characters, excepting the colon :.
However, if you are trying to force a string value (and you may not be), you can perform a sub-expression within the string to evaluate the members of an object, including array-accessors and object properties. You can also perform more complex expressions as well, but for your case below you could leave the quotes and use $() to get the result you want:
"$($Dimensions[$i].value)"
The $() tells the parser to "run this expression and put the result into the string". Again, whatever object is returned will implicitly have ToString() run on it when it gets inserted into the final string, so whatever output $Dimensions[$i].value.ToString() produces will be your string value.

Powershell Append PSCustomObject to nested PSCustomObject

My problem is that I have a remote API giving me a JSON with 10 levels deep nesting and I need to add another array/hashtable/PSCustomObject (whatever you want to call it) to one of them.
The JSON looks like this:
{
"result": [
"modules": [
"data": {
"segments": [
{
"routes": {
"static": [
{
"destination": "10.0.0.0",
"netmask": "255.0.0.0"
},
{
"destination": "172.16.0.0",
"netmask": "255.240.0.0"
}
]
}
}
]
}
]
]
}
The return object is a Hashtable, however when I try to access the values in dot notation, it turns into PSCustomObject:
$statics = $result.result.modules.data.segments.routes.static
It won't let me use the other notation:
$statics = $result['result']['modules']['data']['segments']['routes']['static']
Error: Cannot index into a null array
The real tricky part (for me) is to to append a new hastable to the "static" hashtable in such a way that the rest of the hashtable remains intact.
$newroute = #{
destination = "1.1.1.1."
netmask = "255.255.255.255"
}
I would have used PHP or Python but for this project I must use Powershell and I'm running into all sorts of things where PS behaves different from what I expect.
Any help is greatly appreciated!
static is an array, you can add new items to it:
$newStaticRoute = [pscustomobject]#{
destination = '192.168.0.0'
netmask = '255.255.0.0'
}
$result.result.modules.data.segments.routes.static += $newStaticRoute

Retrieve value from Json object with field having dots and hyphen in powershell

I want to retrieve value of a field present in json object. The filed name has dots and hyphen.
For eg:
$json = #"
{
"Stuffs":
{
"Name.new-name": "Darts",
"Type": "Fun Stuff"
}
}
"#
How can I get the value Darts?
I tried some approaches like
$x = $json | ConvertFrom-Json
$x.Stuffs.(Name.new-name)
But it doesn't work.
Try this
$x.Stuffs.'Name.new-name'

Is it possible to extract a set of database rows with RestTemplate?

I am having difficulties getting multiple datasets out of my database with RestTemplate. I have many routines that extract a single row, with a format like:
IndicatorModel indicatorModel = restTemplate.getForObject(URL + id,
IndicatorModel.class);
and they work fine. However, if I try to extract a set of data, such as:
Map<String, List<S_ServiceCoreTypeModel>> coreTypesMap =
restTemplate.getForObject(URL + id, Map.class);
this returns values in a
Map<String, LinkedHashMap<>>
format. Is there an easy way to return a List<> or Set<> in the desired format?
Fundamentally the issue is that your Java object model does not match the structure of your json document. You are attempting to deserialize a single json element into a java List. Your JSON document looks like:
{
"serviceCoreTypes":[
{
"serviceCoreType":{
"name":"ALL",
"description":"All",
"dateCreated":"2016-06-23 14:46:32.09",
"dateModified":"2016-06-23 14:46:32.09",
"deleted":false,
"id":1
}
},
{
"serviceCoreType":{
"name":"HSI",
"description":"High-speed Internet",
"dateCreated":"2016-06-23 14:47:31.317",
"dateModified":"2016-06-23 14:47:31.317",
"deleted":false,
"id":2
}
}
]
}
But you cannot turn a serviceCoreTypes into a List, you can only turn a Json Array into a List. For instance if you removed the unnecessary wrapper elements from your json and your input document looked like:
[
{
"name": "ALL",
"description": "All",
"dateCreated": "2016-06-23 14:46:32.09",
"dateModified": "2016-06-23 14:46:32.09",
"deleted": false,
"id": 1
},
{
"name": "HSI",
"description": "High-speed Internet",
"dateCreated": "2016-06-23 14:47:31.317",
"dateModified": "2016-06-23 14:47:31.317",
"deleted": false,
"id": 2
}
]
You should be able to then deserialize THAT into a List< S_ServiceCoreTypeModel>. Alternately if you cannot change the json structure, you could create a Java object model that models the json document by creating some wrapper classes. Something like:
class ServiceCoreTypes {
List<ServiceCoreType> serviceCoreTypes;
...
}
class ServiceCoreTypeWrapper {
ServiceCoreType serviceCoreType;
...
}
class ServiceCoreType {
String name;
String description;
...
}
I'm assuming you don't actually mean database, but instead a restful service as you're using RestTemplate
The problem you're facing is that you want to get a Collection back, but the getForObject method can only take in a single type parameter and cannot figure out what the type of the returned collection is.
I'd encourage you to consider using RestTemplate.exchange(...)
which should allow you request for and receive back a collection type.
I have a solution that works, for now at least. I would prefer a solution such as the one proposed by Ben, where I can get the HTTP response body as a list of items in the format I chose, but at least here I can extract each individual item from the JSON node. The code:
S_ServiceCoreTypeModel endModel;
RestTemplate restTemplate = new RestTemplate();
JsonNode node = restTemplate.getForObject(URL, JsonNode.class);
JsonNode allNodes = node.get("serviceCoreTypes");
JsonNode oneNode = allNodes.get(1);
ObjectMapper objectMapper = new ObjectMapper();
endModel = objectMapper.readValue(oneNode.toString(), S_ServiceCoreTypeModel.class);
If anyone has thoughts on how to make Ben's solution work, I would love to hear it.

How to document response fields for an object as Map(HashMap)

I am doing documentation for a REST service returning an object like this:
Map<String, HashMap<Long, String>>
and i find no way to describe response fields for such object.
Let's have a look at my code.
The service:
#RequestMapping(value = "/data", method = RequestMethod.GET)
public Map<String, HashMap<Long, String>> getData()
{
Map<String, HashMap<Long, String>> list = dao.getData();
return list;
}
My unit-test-based documentation:
#Test
public void testData() throws Exception
{
TestUtils.beginTestLog(log, "testData");
RestDocumentationResultHandler document = document(SNIPPET_NAME_PATTERN ,preprocessResponse(prettyPrint()));
document.snippets(
// ,responseFields(
// fieldWithPath("key").description("key description").type("String"),
// fieldWithPath("value").description("value as a Hashmap").type("String"),
// fieldWithPath("value.key").description("value.key description").type("String"),
// fieldWithPath("value.value").description("value.value description").type("String"),
// )
String token = TestUtils.performLogin(mockMvc, "user", "password");
mockMvc
.perform(get(APP_BUILD_NAME + "/svc/data").contextPath(APP_BUILD_NAME)
.header("TOKEN", token)
)
.andExpect(status().is(200))
.andExpect(content().contentType("application/json;charset=UTF-8"))
.andExpect(jsonPath("$").isMap())
.andDo(document);
TestUtils.endTestLog(log, "testData");
}
As you can see the code for response fields is commented out since I haven't had any solution for it yet. I am working on that but i really appreciate your help. Thank you in advance.
Your JSON contains a huge number of different fields. There looks to be over 1000 different entries in the map. Each of those entries is itself a map with a single key-value pair. Those keys all appear to vary as well. Potentially, that gives you over 2000 fields to document:
cancel
cancel.56284
year
year.41685
segment_de_clientele
segment_de_clientele.120705
…
This structure is making it hard to document and is also a strong indicator that it will be hard to consume by clients. Ideally, you would restructure the JSON so that each entry has the same keys and it's only the values the vary from entry to entry. Something like this, for example:
{
"translations": [ {
"name": "cancel",
"id": 56284,
"text": "Exit"
}, {
"name": "year",
"id": 41685,
"text": "Year"
}, {
"name": "segment_de_clientele",
"id": 120705,
"text": "Client segment"
}]
}
This would mean that you only have a handful of fields to document:
translations[]
translations[].name
translations[].id
translations[].text
If that's not possible, then I'd stop trying to use the response fields snippet to document the structure of the response. Instead, you should describe its structure manually in your main Asciidoctor source file.
There are two options:
1> Changing MAP to LIST of objects so that the response fields can be described easily.
2> Putting description manually to index.adoc file.
In my case, I go for option 2 because I have to stick to MAP.