KalenPeterson
Contributor
Contributor

vRA 8.1 How to Parse JSON in Blueprints

Does anyone have examples of how to parse and use JSON input within a Blueprint? I have a JSON payload that will be sent as an input to a blueprint, but I have been unsuccessful in parsing it correctly. I have tried to pass it in as an 'object' input and a 'string' input, but it always seems to come in as a string regardless.

I did see the JSON functions in the expression documentation, but I have not been able to get them to work correctly. I always just end up getting the expression itself passed as a string to the property. Does anyone have an example of using these?

from_json(string)Parse json string
to_json(value)Serialize value as json string
json_path(value, path)Evaluate path against value using XPath for JSON.

https://docs.vmware.com/en/vRealize-Automation/8.1/Using-and-Managing-Cloud-Assembly/GUID-12F0BC64-6...

Here are some examples of what I have tried.

inputs:
  json_string:
    type: string
    default: '{ "servers": [ { "specs": { "cpu": "4", "memory": "16" } } ] }'
  json_object:
    type: object

resources:
  VirtualMachine:
    type: Cloud.vSphere.Machine
    properties:
      cpuCount: '${input.json_object.servers[0].specs.cpu}'
      cpuCount: '${json_path(input.json_string, $.servers.specs.cpu)}'
      cpuCount: '${from_json(input.json_string).servers[0].specs.cpu}'

0 Kudos
4 Replies
lnairn
VMware Employee
VMware Employee

Hi KalenPeterson​,

I think you can use Form Designer (Custom form) with a simple workflow to transform the values you need (I've tested it and is an easy way)

I've tried doing it in Cloud Assembly Blueprint and didn't found a way to do it (Always pass the string variable)

Regards,

Leandro.

0 Kudos
KalenPeterson
Contributor
Contributor

Thanks,

Do you mean using an "External source" for the value I want in Form Designer, and parsing it with a vRO Action? It also looks like I can hand off Validation of a field to a vRO Action.

I will take a look at that later today. I am still curious to figure out how those *_json functions work in the Blueprint expression language. All of my requests for blueprints will be via API so I would like to avoid using any custom forms at all.

Thanks,

Kalen

0 Kudos
KalenPeterson
Contributor
Contributor

I tested using vRO actions to parse this today. It DOES work when you are making the request through the Service Broker Portal, but not when making the request via API. I'm assuming custom form actions are not processed when making a catalog request via API.

I'm going to open a case to see if there is any way to do this. If not, I will work with the team submitting the requests to re-format the request JSON. Will update once resolved.

0 Kudos
j_dubs
Enthusiast
Enthusiast

Hi Kalen,

I had similar use-case this week, whereby trying to use array inputs from the ITSM module - but applies.

We needed to pass an array which wasn't supported, so have the same as you - taking in a json payload as a string.

I was able to parse and use it within the blueprint this way for my case:

 

 

inputs:

diskGrid:
type: string
default: '[{"size":10,"label":"DataGrid as String D","filesystem":"NTFS","mountpoint":"D"}]'

 

Cloud_Volume_1:
type: Cloud.vSphere.Disk
properties:
capacityGb: '${from_json(input.diskGrid).[0].size}'
name: '${input.hostname+"_disk_1"}'
count: '${length(from_json(input.diskGrid)) >= 1 ? 1 : 0 }'
mountpoint: '${from_json(input.diskGrid).[0].mountpoint}'

 

Cloud_Volume_2:
type: Cloud.vSphere.Disk
dependsOn:
- Cloud_Volume_1
properties:
capacityGb: '${from_json(input.diskGrid).[1].size}'
name: '${input.hostname+"_disk_1"}'
count: '${length(from_json(input.diskGrid)) >= 2 ? 1 : 0 }'
mountpoint: '${from_json(input.diskGrid).[1].mountpoint}'

0 Kudos