populate json obect array into df table using pyspark? - json

I have values for a column in a df coming as json objects array
[
{
"name": "ben",
"apartment": "dfg9",
"isitflag": false,
"iscruxflag": false,
"hisindexnum": 2,
"valueinb": "110jb",
},
{
"name": "bol",
"apartment": "ran",
"isitflag": false,
"iscruxflag": false,
"hisindexnum": 2,
"valueinb": "110jb"
},
{
"name": "dol",
"apartment": "hele",
"isitflag": false,
"iscruxflag": false,
"hisindexnum": 2,
"valueinb": "110jb"
}
]
I would need to take this value column from previous pd and populate it into a new table with those keys as indes of the table and values as rows in pyspark?
I understand this question seems like lack of trying but as a newbie in pyspark, tried few things but looking if any pyspark expert to steer in guidance

Related

Sort by nested fields in json in Power Automate

I'm trying to sort a JSON String in Power Automate by a nested field called "orderHint".
My JSON String looks like this:
[
{
"id": "5134",
"value": {
"isChecked": false,
"title": "This is another test",
"orderHint": "8585298133570680672PF"
},
"lastModifiedDateTime": "2022-12-23T11:06:28.4256622Z"
},
{
"id": "26576",
"value": {
"isChecked": true,
"title": "This is a test",
"orderHint": "8585498133570680672DE"
},
"lastModifiedDateTime": "2022-12-23T11:06:28.4256622Z"
}
]
When I'm trying to sort by "orderHint", I get an error:
"'The template language function 'sort' did not find the named sortField 'orderHint' on one or more objects in the array."
I'm using the following expression:
sort(variables('varArrayChecked'), 'value/orderHint')
Sorting by other fields works fine, e.g.:
sort(variables('varArrayChecked'), 'id')
Is there any way how I can sort by a nested field in a JSON String?
Thanks in advance!
You can use the Advanced Data Operations connector as it will do it for you in a single step.
The Flatten Object Array step is perfect for the payload you've provided.
You can see that it will take the data, flatten it and you have the ability to sort it on the way out (noting that the Array variable contains the exact JSON you provided in your question) ...
Note: Balance Output must be set to true in order for the sorting to occur.
Result
This is the resulting JSON order by orderHint ascending.
[
{
"id": "5134",
"lastModifiedDateTime": "2022-12-23T11:06:28",
"value/isChecked": false,
"value/orderHint": "8585298133570680672PF",
"value/title": "This is another test"
},
{
"id": "26576",
"lastModifiedDateTime": "2022-12-23T11:06:28",
"value/isChecked": true,
"value/orderHint": "8585498133570680672DE",
"value/title": "This is a test"
}
]
... and to show it in descending order (which is obvious, but simply change the sort order object value from Asc to Desc) ...
[
{
"id": "26576",
"lastModifiedDateTime": "2022-12-23T11:06:28",
"value/isChecked": true,
"value/orderHint": "8585498133570680672DE",
"value/title": "This is a test"
},
{
"id": "5134",
"lastModifiedDateTime": "2022-12-23T11:06:28",
"value/isChecked": false,
"value/orderHint": "8585298133570680672PF",
"value/title": "This is another test"
}
]

Ansible trouble parsing JSON to get correct UUIDs to poweron VMs

making an API GET cal I get the following JSON structure:
{
"metadata": {
"grand_total_entities": 231,
"total_entities": 0,
"count": 231
},
"entities": [
{
"allow_live_migrate": true,
"gpus_assigned": false,
"ha_priority": 0,
"memory_mb": 1024,
"name": "test-ansible2",
"num_cores_per_vcpu": 2,
"num_vcpus": 1,
"power_state": "off",
"timezone": "UTC",
"uuid": "e1aff9d4-c834-4515-8c08-235d1674a47b",
"vm_features": {
"AGENT_VM": false
},
"vm_logical_timestamp": 1
},
{
"allow_live_migrate": true,
"gpus_assigned": false,
"ha_priority": 0,
"memory_mb": 1024,
"name": "test-ansible1",
"num_cores_per_vcpu": 1,
"num_vcpus": 1,
"power_state": "off",
"timezone": "UTC",
"uuid": "4b3b315e-f313-43bb-941b-03c298937b4d",
"vm_features": {
"AGENT_VM": false
},
"vm_logical_timestamp": 1
},
{
"allow_live_migrate": true,
"gpus_assigned": false,
"ha_priority": 0,
"memory_mb": 4096,
"name": "test",
"num_cores_per_vcpu": 1,
"num_vcpus": 2,
"power_state": "off",
"timezone": "UTC",
"uuid": "fbe9a1ac-cf45-4efa-9d65-b3257548a9f4",
"vm_features": {
"AGENT_VM": false
},
"vm_logical_timestamp": 17
},
]
}
In my Ansible playbook I register a variable holding this content.
I need to get a list of UUID of "test-ansible1" and "test-ansible2" but I'm having a hard time finding the best way to to this.
Note that I have another variable holding the list of names for which I need to lookup the UUID.
The need is to use those UUIDs to fire a poweron command for all UUIDs corresponding to specific names.
How would you guys do that?
I've taken a number of approaches but I can't seem to get what I want so I prefer an uninfluenced opinion.
P.S.: This is what Nutanix AHV returns as a get of all vms thgough APIs. There seems to me no way to get only specific VMs JSON information but only all VMs.
Thanks.
Here is some Jinja2 magic for you:
- debug:
msg: "{{ mynames | map('extract', dict(test_json | json_query('entities[].[name,uuid]'))) | list }}"
vars:
mynames:
- test-ansible1
- test-ansible2
Explanation:
test_json | json_query('entities[].[name,uuid]') reduces your original json data to a list of elements which are lists of two items – name value and uuid value:
[
[
"test-ansible2",
"e1aff9d4-c834-4515-8c08-235d1674a47b"
],
[
"test-ansible1",
"4b3b315e-f313-43bb-941b-03c298937b4d"
],
[
"test",
"fbe9a1ac-cf45-4efa-9d65-b3257548a9f4"
]
]
BTW you can use http://jmespath.org/ to test query statements.
dict(...) when applied to such structure (list of "touples") generates a dictionary:
{
"test": "fbe9a1ac-cf45-4efa-9d65-b3257548a9f4",
"test-ansible1": "4b3b315e-f313-43bb-941b-03c298937b4d",
"test-ansible2": "e1aff9d4-c834-4515-8c08-235d1674a47b"
}
Then we apply extract filter as per documentation to fetch only required elements:
[
"4b3b315e-f313-43bb-941b-03c298937b4d",
"e1aff9d4-c834-4515-8c08-235d1674a47b"
]

Accessing JSON array within JSON object in VBA Excel using VBA-JSON

I am working on VBA macros to access live data from a website through API calls. I am using VBA-JSON library for it. There are mainly two scenarios when fetching data:
Scenario #1 (we can see data is a JSON array):
{
"success": true,
"data": [
{
"id": 4,
"creator_user_id": {
"id": 162,
"name": "ASD",
"email": "email id"
},
"user_id": {
"id": 787878,
"name": "XYZ",
"email": "email id"
},
...
}
]
}
In the scenario 1 I can fetch values within data array by using For Each loop in VBA Excel through VBA-JSON library.
Scenario #2:
{
"success": true,
"data": {
"id": 123,
"name": "ABC",
"options": [
{
"id": 119,
"label": "Name1"
},
{
"id": 120,
"label": "Name2"
}
],
"mandatory_flag": false
}
}
But in 2nd scenario I can not access data within data array, because JSON data array is within JSON object. For example I want the value of id: 120 which must return Name2, thus I want to access value of label which will return Name2.
I tried many ways, but can not get it. Please anyone can tell me how this can be done in VBA Excel?
Any help appreciated.
Using the VBA-JSON library, you can access the value you want by calling:
Set Parsed = JsonConverter.ParseJson(<yourjsonstring>)
Debug.Print Parsed("data")("options")(2)("label")

dynamic json for datatables

I've been working with datatables and I'm able to load the datatable using getJson with strongly typed classes etc and it works just great. Until I hit one snag.
There are times I want to populate a datatable with data that "I don't know about" but I always know that it will be one row of data - it is simply a json string with dynamic content.
Now with datatables you can simply populate the table with aaData and aaCol by assigning a json string to it but my json string contains a column and data IE:
First_name:bob and so on.
A column - on Datatables would be populated with sTitle:Column1 etc and assigned to aaCol.
Does anyone know of a plug in that parses a json string into aaCol and aaData for use with datatables?
I believe you can solve your problem using this approach:
$(document).ready(function() {
$('#example').DataTable( {
"processing": true,
"serverSide": true,
"ajax": "scripts/objects.php",
"columns": [
{ "data": "first_name" },
{ "data": "last_name" },
{ "data": "position" },
{ "data": "office" },
{ "data": "start_date" },
{ "data": "salary" }
]
} );
} );
In the example above the dataTables uses a serverSide processing, the ajax return a object like this:
{
"draw": 1,
"recordsTotal": 57,
"recordsFiltered": 57,
"data": [
{
"first_name": "Airi",
"last_name": "Satou",
"position": "Accountant",
"office": "Tokyo",
"start_date": "28th Nov 08",
"salary": "$162,700"
}, ...
You can also set the column name using the "name" property inside the specification of each column.
You can the full example in the following link. If you need more assistance I can make a code on my own later today =)
You can check this JsFiddle to understand how to set the columns names

jqgrid subgrids each show all items from the json data

I've been trying to implement a simple subgrid within jqgrid to show line items for an invoice. I finally got the subgrids to populate but each subgrid is showing the same list of line items, which is actually all of the entries in the data set.
I'm not quite sure how to debug this but here are some of my potential ideas-
Is it a problem with the way the json store is (not) responding to the GET queries?
Is it because nowhere I define what field within the subgrid data is the "foriegn key" so to speak.
Do I need the subGridUrl to point to json data with only the appropriate data (not every line item)
Example JSON for line items:
order_id points to the id of the order
{
"total": 1,
"records": 6,
"rows": [
{
"description": "PART X",
"order_id": 2,
"qty": 5,
... more fields ...
"id": 1
},
... more ...
],
page: 1
}
JSON for main grid items:
{
"total": 1,
"records": 2,
"rows": [
{
"order_no": 2,
... more fields ...
"id": 2
},
... more ...
],
page:1
}
Applicable parts of my jqqrid script:
jQuery("#mygrid").jqGrid({
... cosmetic stuff for main grid ...
url: "/my_json_url/",
datatype: "json",
colNames:['Order',...],
colModel:[
{name:'order_no', index:'order_no'},
...
],
jsonReader: {
repeatitems:false,
root: "rows",
page: "page",
total: "total",
records: "records",
cell: "",
id: "id",
subgrid: {root: "rows", cell:"", repeatitems: false}
},
prmNames: {subgridid: "order_id"},
subGrid: true,
subGridUrl: "/json_url/to_line_items/",
subGridModel: [{ name : ['qty','description'],
width: [100,100] }]
})navGrid(some options);
I suppose that the code under the URL "/json_url/to_line_items/" don't use id parameter sent by jqGrid. If the user expand the subgrid the rowid of the row will be used as additional parameter of subGridUrl. By the way I don't understand why you use id values of the grid other as the order_id. Currently the id=1 parameter will be appended to the subGridUrl in case of expanding the row with order_id=10. Is it what you want?