VMware Networking Community
joshuabmaurer
Contributor
Contributor

NSX JSON Field Queries

Feeling like a noob here.

The goal of this exercise is to extract a list of VLAN segments and then to extract a list of guest VMs attached to these segments from the Local NSX Manager. With this information move those guest VMs to the Global NSX Manager VLAN segment.

API call, list of segments: GET http://{{local-nsx-mgr}}/policy/api/v1/infra/segments.

API call, list of segment guest: GET http://{{local-nsx-mgr}}/policy/api/v1/infra/segments/{{vlan-seg}}/port

This produced the json files: seg-list.json and vlan-seg-01.json. Eventually there will be more vlan-seg files once I get this rolling.

My delema is that I typically work from CSV files, running them through a for loop to create the over all CSV file to work from to push the configuration out.

I am stumped on how to work with the json file. For the seg-list.json, I want the "id" ( seg-vlan name ) collected to run the list against ports for server collections.

Tried to use a JSON to CSV vscode tool and Excel Get Data > From File > From JSON with no success. I do not know how to work with this JSON file. Any help or direction is greatly appreciated.

I was trying to do this work in PowerCLI but not sure if its my system or PowerCLI that was not allowing the queries. Also not sure if I would have received the data any differently.

Thank you.

0 Kudos
1 Reply
grahamrodrigue
Contributor
Contributor


@joshuabmaurer wrote:

Feeling like a noob here.

The goal of this exercise is to extract a list of VLAN segments and then to extract a list of guest VMs attached to these segments from the Local NSX Manager. With this information move those guest VMs to the Global NSX Manager VLAN segment.

API call, list of segments: GET http://{{local-nsx-mgr}}/policy/api/v1/infra/segments.

API call, list of segment guest: GET http://{{local-nsx-mgr}}/policy/api/v1/infra/segments/{{vlan-seg}}/port

This produced the json files: seg-list.json and vlan-seg-01.json. Eventually there will be more vlan-seg files once I get this rolling.

My delema is that I typically work from CSV files, running them through a for loop to create the over all CSV file to work from to push the configuration out.

I am stumped on how to work with the json file. For the seg-list.json, I want the "id" ( seg-vlan name ) collected to run the list against ports for server collections.

Tried to use a JSON to CSV vscode tool and Excel Get Data > From File > From JSON with no success. I do not know how to work with this JSON file. Any help or direction is greatly appreciated.

I was trying to do this work in PowerCLI but not sure if its my system or PowerCLI that was not allowing the queries. Also not sure if I would have received the data any differently.

Thank you.


Hello,

To work with JSON files and extract the necessary data, you can use a scripting language like Python, which has built-in support for JSON. Here’s a short example of how you could extract the “id” from your seg-list.json and then use it to get the ports for server collections:

import json

# Load the segments list
with open('seg-list.json', 'r') as file:
segments = json.load(file)

# Extract the segment IDs
segment_ids = [seg['id'] for seg in segments]

# Now you can use segment_ids to make further API calls
# and collect ports for server collections

This script opens your seg-list.json, reads the data, and then uses a list comprehension to create a list of segment IDs. You can then use this list to iterate over and make the necessary API calls to collect more data.

For converting JSON to CSV, you can use Python’s csv module in combination with the json module to parse the JSON and write it to a CSV file. Here’s a basic structure for that:

import csv
import json

# Load the JSON data
with open('seg-list.json', 'r') as file:
data = json.load(file)

# Open a CSV file for writing
with open('seg-list.csv', 'w', newline='') as file:
writer = csv.writer(file)

# Write the header row
writer.writerow(['id', 'other_field1', 'other_field2'])

# Write the JSON data to the CSV
for entry in data:
writer.writerow([entry['id'], entry['other_field1'], entry['other_field2']])

This script assumes that your JSON objects have fields named 'id', 'other_field1' and 'other_field2'. You'll need to adjust the field names to match those in your actual JSON data. 

 

 

Bets Regard,
grahamrodrigue
Doglikesbest
0 Kudos