https://pulumi.com logo
Title
r

rhythmic-crowd-40243

11/07/2022, 2:02 AM
Hello All. I’m trying to read an output serialized as json from a stack upstream. My output looks something like this:
"logWorkspaces": {
    "eastus2": "app-logs-eastus2-law16df9848",
    "central": "...",
    ...
}
This my attempt to try and extract the output.
var logWorkspaceReference =  coreStackReference
                    .RequireOutput(App.Infra.Core.OutputNames.LogWorkspace);

var x = logWorkspaceReference.Apply(o => JsonSerializer
    .Deserialize<Dictionary<string, object>>(o.ToString()));

var workspace = Pulumi.AzureNative.OperationalInsights.GetWorkspace.Invoke(
    new GetWorkspaceInvokeArgs
    {
        ResourceGroupName = resourceGroup.Location,
        WorkspaceName = x.Apply(url => (string) url["eastus2"])
    });
But deserialization is failing…
an unhandled exception:
    System.Text.Json.JsonException: 'S' is an invalid start of a value. Path: $ | LineNumber: 0 | BytePositionInLine: 0.
       at void System.Text.Json.ThrowHelper.ReThrowWithPath(ref ReadStack state, JsonReaderException ex)
I don’t think I’m understanding outputs correctly. But before I spend anymore time on this, can this be done?
e

echoing-dinner-19531

11/07/2022, 9:48 AM
I don't think you need the json deserialize step, logWorkspaceReference should already be a dictionary object.
r

rhythmic-crowd-40243

11/07/2022, 12:30 PM
I see. Do you know how I would go about extracting the value from the object returned? There doesn’t appear to be a trivial way to covert an object to a dictionary. I’m wondering is there a Pulumi best practices way to accomplish this?
e

echoing-dinner-19531

11/07/2022, 12:41 PM
I think you should just cast it there. Like
logWorkspaceReference.Apply(x => (Dictionary<string, object>)x)
, I think structured values are due a pass over making them better at some point though. Like all this is just JSON underneath we should just let RequireOutput set the JSON deserialiser and return the right value there and then.
r

rhythmic-crowd-40243

11/07/2022, 11:46 PM
logWorkspaceReference.Apply(x => (ImmutableDictionary<string, object>)x)
worked. I don’t know why I wasn’t connecting the dots, but I’m moving again. The future state of
RequireOutput
sounds great, looking forward to its eventual arrival. Thank you so much @echoing-dinner-19531!