convert pandas dataframe to json with specific format - json

I am trying to convert the dataframe mentioned below to desired json
enter image description here
column_id,column_name,mandatory,column_data_type,column_data_length,_id,data_format,file_type,active_ind
1,PAT_ID,FALSE,VARCHAR,2500,5f2193c39448c44f0c1b65e0,TEXT,FACT,TRUE
2,PAT_NAME,FALSE,VARCHAR,2500,5f2193c39448c44f0c1b65e0,TEXT,FACT,TRUE
3,PAT_AGE,FALSE,VARCHAR,2500,5f2193c39448c44f0c1b65e0,TEXT,FACT,TRUE
Like the json mentioned below
{
"_id": 5f2193c39448c44f0c1b65e0,
"data_format": "TEXT",
"file_type": "FACT",
"columns": [
{
"column_id": 1,
"column_name": "PAT_ID",
"mandatory": "false",
"column_data_type": "VARCHAR",
"column_data_length": 2500
},
{
"column_id": 2,
"column_name": "PAT_NAME",
"mandatory": "false",
"column_data_type": "VARCHAR",
"column_data_length": 2500
}
],
"active_ind": "true",
}
I tried to group by based on column name and column id with so many methods
this will group column but not all the values
cac= df.groupby('column_id').apply(lambda x: x.to_json(orient='records'))
cac = df.to_json(orient='records')
I am not able to separate the id and columns.
Please help me on this

here is what I would do:
# Load data
df = pd.read_csv('data.csv')
# Create list of dict for columns column
col_set = ['column_id',
'column_name',
'mandatory',
'column_data_type',
'column_data_length']
df['columns'] = df[col_set].apply(lambda x: x.to_dict(), axis=1)
reorder = ['column_id',
'column_name',
'mandatory',
'column_data_type',
'column_data_length',
'columns',
'active_ind',
'_id',
'data_format',
'file_type']
df = df[reorder]
# Group by similar rows and join sub dicts
col_set_2 = ['_id', 'data_format', 'file_type', 'columns', 'active_ind']
col_set_3 = ['_id', 'data_format', 'file_type', 'active_ind']
df2 = df[col_set_2].groupby(col_set_3)['columns'].apply(lambda x: list(x)).reset_index()
df2 = df2[col_set_2]
# Dataframe to json
parsed = json.loads(df2.to_json(orient='records', indent=4))
result = json.dumps(parsed[0], indent=4)
print(result)
{
"_id": "5f2193c39448c44f0c1b65e0",
"data_format": "TEXT",
"file_type": "FACT",
"columns": [
{
"column_id": 1,
"column_name": "PAT_ID",
"mandatory": false,
"column_data_type": "VARCHAR",
"column_data_length": 2500
},
{
"column_id": 2,
"column_name": "PAT_NAME",
"mandatory": false,
"column_data_type": "VARCHAR",
"column_data_length": 2500
},
{
"column_id": 3,
"column_name": "PAT_AGE",
"mandatory": false,
"column_data_type": "VARCHAR",
"column_data_length": 2500
}
],
"active_ind": true
}

Related

How to use json_normalize to create subcolumns in csv file

I need some help on using json_normalizer to create multiple subcolumns from main columns ? The below code is able to generate an output file but its puts everything in one column however i need something seperate columns with heading like moniker.config, moniker.type, moniker.key, moniker.keyparts for each row
from pandas.io.json import json_normalize
import os
import pandas as pd
def json_normalize_recursive(base_column, data, df=pd.DataFrame()):
if df.empty:
df = json_normalize(data, record_prefix=base_column+'.')
nested = df.select_dtypes(include='object')
for col in nested.columns:
try:
nested_df = json_normalize(nested[col].tolist())
nested_df.columns = [base_column+'.'+str(col)+'.'+str(c) for c in nested_df.columns]
df = pd.concat([df.drop(col, axis=1), nested_df], axis=1)
except ValueError:
pass
return df
data = {
"errors":[
],
"data":[
{
"moniker":{
"config":"fx.ipv.london.eod",
"type":"fx.spot",
"key":"EUR/CZK",
"keyParts":[
"EUR",
"CZK"
],
"configType":"fx.ipv.london.eod/fx.spot",
"live":True
},
"queryMoniker":{
"config":"fx.ipv.london.eod",
"type":"EUR/CZK",
"key":"EUR/CZK",
"tag":{
"owner":"official",
"type":"fx.spot",
"key":"EUR/CZK",
"tag":{
"owner":"official",
"date":13434324400999,
"cutoff":"London",
"name":"ipv",
"live":True
},
"keyParts":[
"EUR",
"CZK"
],
"configType":"fx.ipv.london.eod/fx.spot",
"live":False
},
"instance":{
"data":"<FxSpot Currency1=\"EUR\"Currency2=\"CZK\" bid=\"24.14\" ask=\"24.147\"/>",
"unmarshalled":True,
"marshalled":True,
"format":"fx/xml/1",
"valid":True,
"sequence":1643434234234,
"instanceMoniker":{
"source":"viper.tagcopy",
"config":"fx.london.official.copy",
"keyParts":[
"EUR",
"CZK"
]
}
}
}
}
]
}
df = json_normalize_recursive('', data)
print(df)
cwd = os.getcwd()
filepath = os.path.join(cwd, 'Desktop', 'output.csv')
df.to_csv(filepath, index=False)
Desired output:
try using df = pd.json_normalize(data,'data'['monier','queryMonier']
You can try:
data = {
"errors": [],
"data": [
{
"moniker": {
"config": "fx.ipv.london.eod",
"type": "fx.spot",
"key": "EUR/CZK",
"keyParts": ["EUR", "CZK"],
"configType": "fx.ipv.london.eod/fx.spot",
"live": True,
},
"queryMoniker": {
"config": "fx.ipv.london.eod",
"type": "EUR/CZK",
"key": "EUR/CZK",
"tag": {
"owner": "official",
"type": "fx.spot",
"key": "EUR/CZK",
"tag": {
"owner": "official",
"date": 13434324400999,
"cutoff": "London",
"name": "ipv",
"live": True,
},
"keyParts": ["EUR", "CZK"],
"configType": "fx.ipv.london.eod/fx.spot",
"live": False,
},
"instance": {
"data": '<FxSpot Currency1="EUR"Currency2="CZK" bid="24.14" ask="24.147"/>',
"unmarshalled": True,
"marshalled": True,
"format": "fx/xml/1",
"valid": True,
"sequence": 1643434234234,
"instanceMoniker": {
"source": "viper.tagcopy",
"config": "fx.london.official.copy",
"keyParts": ["EUR", "CZK"],
},
},
},
}
],
}
df = pd.DataFrame(data['data'])
df = pd.concat([df, df.pop('moniker').apply(pd.Series).add_prefix('moniker.')], axis=1)
df = pd.concat([df, df.pop('queryMoniker').apply(pd.Series).add_prefix('queryMoniker.')], axis=1)
df = pd.concat([df, df.pop('queryMoniker.tag').apply(pd.Series).add_prefix('queryMoniker.tag.')], axis=1)
df = pd.concat([df, df.pop('queryMoniker.instance').apply(pd.Series).add_prefix('queryMoniker.instance.')], axis=1)
df = df.explode('moniker.keyParts')
print(df)
Prints:
moniker.config moniker.type moniker.key moniker.keyParts moniker.configType moniker.live queryMoniker.config queryMoniker.type queryMoniker.key queryMoniker.tag.owner queryMoniker.tag.type queryMoniker.tag.key queryMoniker.tag.tag queryMoniker.tag.keyParts queryMoniker.tag.configType queryMoniker.tag.live queryMoniker.instance.data queryMoniker.instance.unmarshalled queryMoniker.instance.marshalled queryMoniker.instance.format queryMoniker.instance.valid queryMoniker.instance.sequence queryMoniker.instance.instanceMoniker
0 fx.ipv.london.eod fx.spot EUR/CZK EUR fx.ipv.london.eod/fx.spot True fx.ipv.london.eod EUR/CZK EUR/CZK official fx.spot EUR/CZK {'owner': 'official', 'date': 13434324400999, 'cutoff': 'London', 'name': 'ipv', 'live': True} [EUR, CZK] fx.ipv.london.eod/fx.spot False <FxSpot Currency1="EUR"Currency2="CZK" bid="24.14" ask="24.147"/> True True fx/xml/1 True 1643434234234 {'source': 'viper.tagcopy', 'config': 'fx.london.official.copy', 'keyParts': ['EUR', 'CZK']}
0 fx.ipv.london.eod fx.spot EUR/CZK CZK fx.ipv.london.eod/fx.spot True fx.ipv.london.eod EUR/CZK EUR/CZK official fx.spot EUR/CZK {'owner': 'official', 'date': 13434324400999, 'cutoff': 'London', 'name': 'ipv', 'live': True} [EUR, CZK] fx.ipv.london.eod/fx.spot False <FxSpot Currency1="EUR"Currency2="CZK" bid="24.14" ask="24.147"/> True True fx/xml/1 True 1643434234234 {'source': 'viper.tagcopy', 'config': 'fx.london.official.copy', 'keyParts': ['EUR', 'CZK']}

replace specific keys in a deeply embedded json file

I have a json file (origin.json) generated locally, I'd like to replace some keys in this origin.json and generate a remote.json so that I could send it to a remote server following it's endpoint payload format.
My origin.json is large and deeply embedded , I could iterate each keys and replace those I need to. But I am wondering is there an efficient and fancy tool could do the same ? Something like jq ?
Below are my embedded json
{
"timeoutMs": 3000,
"requestTopic": "local-cron",
"searchQuery": {
"checkin": "2023-01-10",
"checkout": "2023-01-11",
"numberRoomsNeeded": 0,
"adultsTotal": 2,
"childrenTotal": 0,
"currency": "EUR"
},
"requestContext": {
"userId": 666666666,
"userAuthLevel": 2,
"isUserstar": true,
"visitorCc1": "cn",
"trafficSourceId": 0,
"siteTypeId": 9,
"detectedUserType": "normal",
"travelPurpose": 2,
"affiliateId": 12345,
"languageCode": "en-us",
"currency": "CNY",
"siteType": 1,
"serverRole": "cron",
"action": "bp",
"visitorIdentifier": [
{
"type": "id-single",
"uvi": "00000000000000000000000"
},
{
"type": "user-identity",
"uvi": "66666666"
},
{
"type": "user",
"uvi": "77777777777"
}
],
"isInternal": true,
"enableExperiments": true,
"shouldTrackRequestInExperiments": true,
"starSettings": {
"isUserstar": true,
"isUserstarControlGroup": false,
"canStarUserSeeFreeBreakfast": true,
"canStarUserSeeFreeRoomUpgrade": true,
"starTier": 5,
"topstarBenefit": "",
"isRightstar": true,
"starDynamicPricing": {
"canSeestarDynamicPricingLevel3": true
},
"canStarUserSeeFreeCleaningFee": true,
"starVipSettings": [
{
"eligible": true,
"benefitName": "no_et",
"programType": "PriceMatchTrial",
"percentage": 0
}
]
},
"isCsRelocationMode": false,
"tripValueContext": {},
"visitorCountryRegion": "sh",
"paymentTiming": 1,
"includeConditional": false
},
"showDebug": false,
"hits": [
{
"hhhhid": 8228082,
"ppblock": {
"allMatchingBlocks": [
{
"blockId": 1,
"rawBlock": {
"occupancy": 2,
"price": 34425,
"roomId": 822808201,
"policygroupId": 346547507,
"mealplan": 2,
"channel": 581,
"currencyId": 2,
"maxPersons": 3,
"flags": 0,
"freeCancelUntil": 0,
"priceBase10Exponent": -2,
"packageId": 0,
"paymenttermsId": 38,
"vrFlags": 0,
"bundleId": 0
},
"blockStay": {
"stayNights": [
{
"polId": 346547507,
"rateId": 25728208,
"curId": 2,
"price": 344.25,
"price1": 0,
"channelId": 581,
"occupancy": 2,
"roomId": 822808201,
"initialPrice": 405,
"initialPrice1": 0
}
],
"stayNrRooms": 1,
"stayAvailableUntil": 1956105,
"stayPrice": 344.25,
"stayFlashDeal": 0,
"stayPromoTextId": 0,
"stayMinAdvanceRes": 1673388000,
"stayInventorySegmentId": 0,
"stayExperimentFlags": 0,
"stayRoomRateFlags": 4,
"stayIncludedProducts": 0
}
}
]
},
"selectedBlocks": [
"822808201_346547507_2_2_0"
],
"selected": {
"822808201_346547507_2_2_0": 1
}
}
],
"pipeline": 3
}
Here I flagged severval keys I'd like to replace with '==> (new key)'
jq '.. | keys?' star-dragongate.json
[
"hits",
"pipeline",
"requestContext",
"requestTopic",
"searchQuery",
"showDebug", ==> showdebug
"timeoutMs"
]
[
"adultsTotal",
"checkin",
"checkout",
"childrenTotal",
"currency",
"numberRoomsNeeded"
]
[
"action",
"affiliateId", ==> Affilateid
"currency",
"detectedUserType",
"enableExperiments",
"starSettings",
"includeConditional",
"isCsRelocationMode",
"isInternal",
"isUserstar",
"languageCode",
"paymentTiming",
"serverRole",
"shouldTrackRequestInExperiments", ==> inexperiments
"siteType",
"siteTypeId",
"trafficSourceId",
"travelPurpose",
"tripValueContext",
"userAuthLevel",
"userId",
"visitorCc1",
"visitorCountryRegion",
"visitorIdentifier"
]
[
0,
1,
2
]
[
"type",
"uvi"
]
[
"type",
"uvi"
]
[
"type",
"uvi"
]
[
"canStarUserSeeFreeBreakfast",
"canStarUserSeeFreeCleaningFee",
"canStarUserSeeFreeRoomUpgrade", ==> freeroom_upgrade
"starDynamicPricing",
"starTier",
"starVipSettings",
"isRightstar",
"isUserstar",
"isUserstarControlGroup",
"topstarBenefit"
]
[
"canSeestarDynamicPricingLevel3"
]
[
0
]
[
"benefitName",
"eligible",
"percentage",
"programType"
]
[]
[
0
]
[
"hhhhid",
"ppblock",
"selected",
"selectedBlocks"
]
[
"allMatchingBlocks"
]
[
0
]
[
"blockId",
"blockStay",
"rawBlock"
]
[
"bundleId", ==> bundle_id
"channel",
"currencyId",
"flags",
"freeCancelUntil",
"maxPersons",
"mealplan",
"occupancy",
"packageId",
"paymenttermsId",
"policygroupId",
"price",
"priceBase10Exponent",
"roomId",
"vrFlags"
]
[
"stayAvailableUntil",
"stayExperimentFlags",
"stayFlashDeal",
"stayIncludedProducts",
"stayInventorySegmentId",
"stayMinAdvanceRes",
"stayNights",
"stayNrRooms",
"stayPrice",
"stayPromoTextId", ==> staypromotextid
"stayRoomRateFlags"
]
[
0
]
[
"channelId",
"curId",
"initialPrice",
"initialPrice1",
"occupancy",
"polId",
"price",
"price1",
"rateId",
"roomId"
]
[
0
]
[
"822808201_346547507_2_2_0"
]
The keys I need to replace located in different embed layer and blocks of this json.
Is there any suggestion on how to replace those keys in an efficient way ?
PS: The keys in json file are in static location, not dymanically change all the time.
To rename a field name, you could use with_entries, which gives you access to each .key. Reset it by assignment.
Now, what's still unclear is how you want to find the keys in question (programmatically). If their locations are static, and you know them, address them individually, as in:
.requestContext.starSettings |= with_entries((.key | select(. == "canStarUserSeeFreeRoomUpgrade")) = "freeroom_upgrade")
| .hits[].ppblock.allMatchingBlocks[].rawBlock |= with_entries((.key | select(. == "bundleId")) = "bundle_id")
# and so on...
To reduce redundant code, you could also move the renaming portion of it into its own function, and then just call that, e.g.:
def rename($old; $new):
with_entries((.key | select(. == $old)) = $new);
.requestContext.starSettings |= rename("canStarUserSeeFreeRoomUpgrade"; "freeroom_upgrade")
| .hits[].ppblock.allMatchingBlocks[].rawBlock |= rename("bundleId"; "bundle_id")
# and so on...
Or move the location also into the function, e.g.:
def rename_at(path; $old; $new):
path |= with_entries((.key | select(. == $old)) = $new);
rename_at(.requestContext.starSettings; "canStarUserSeeFreeRoomUpgrade"; "freeroom_upgrade")
| rename_at(.hits[].ppblock.allMatchingBlocks[].rawBlock; "bundleId"; "bundle_id")
# and so on...
If their location is unknown, and you want to replace them just based on their (local) name, you need to traverse the document, and check if you hit a matching name. The walk function provides you with the traversal, objects reduces the action to objects:
walk(objects |= with_entries(
if .key == "canStarUserSeeFreeRoomUpgrade" then .key = "freeroom_upgrade"
elif .key == "bundleId" then .key = "bundle_id"
# and so on...
else . end
))

pandas column to list for a json file

from a Dataframe, I want to have a JSON output file with one key having a list:
Expected output:
[
{
"model": "xx",
"id": 1,
"name": "xyz",
"categories": [1,2],
},
{
...
},
]
What I have:
[
{
"model": "xx",
"id": 1,
"name": "xyz",
"categories": "1,2",
},
{
...
},
]
The actual code is :
df = pd.read_excel('data_threated.xlsx')
result = df.reset_index(drop=True).to_json("output_json.json", orient='records')
parsed = json.dumps(result)
jsonfile = open("output_json.json", 'r')
data = json.load(jsonfile)
How can I achive this easily?
EDIT:
print(df['categories'].unique().tolist())
['1,2,3', 1, nan, '1,2,3,6', 9, 8, 11, 4, 5, 2, '1,2,3,4,5,6,7,8,9']
You can use:
df = pd.read_excel('data_threated.xlsx').reset_index(drop=True)
df['categories'] = df['categories'].apply(lambda x: [int(i) for i in x.split(',')] if isinstance(x, str) else '')
df.to_json('output.json', orient='records', indent=4)
Content of output.json
[
{
"model":"xx",
"id":1,
"name":"xyz",
"categories":[
1,
2
]
}
]
Note you can also use:
df['categories'] = pd.eval(df['categories'])

How to create this below JSON response using python DRF [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed last year.
Improve this question
Since I am new to this DRF I am facing some difficulties creating this JSON. I have created an API endpoint and the output of that API endpoint is like this now as shown below
"meta": {
"limit": 1,
"page_count": 1,
"total_count": 1
},
"results": {
"id": 1234567,
"curriculum": {
"ES Math": [
{
"grade_level": "ES",
"subject": "Math",
"subject_abbr": "Math",
"product_id": 438,
"product_title": "Test1",
"ratings": [
{
"source": "A",
"report_url": "********",
"Org1_rating": [
{
"name": "green_alignment_ratings",
"value": 12,
"label": "Meet Expectations",
"label_color": "Green"
},
{
"name": "green_usability_ratings",
"value": 12,
"label": "Meet Expectations",
"label_color": "Green"
}
],
"Org2_rating": [
{
"name": "Overall",
"value": null,
"label": "Tier 1: Exemplifies Quality",
"label_color": null
}
]
}
]
},
{
"grade_level": "ES",
"subject": "Math",
"subject_abbr": "Math",
"product_id": 2085,
"product_title": "Test2",
"ratings": [
{
"source": "A",
"report_url": "********",
"Org1_rating": [
{
"name": "green_alignment_ratings",
"value": 12,
"label": "Meet Expectations",
"label_color": "Green"
},
{
"name": "green_usability_ratings",
"value": 12,
"label": "Meet Expectations",
"label_color": "Green"
}
],
"Org_rating2": "null"
}
]
}
]
}
}
}
But I want the output to be in this format below
{
"meta": {
"limit": 1,
"page_count": 1,
"total_count": 1
},
"results": {
"id": 1234567,
"curriculum": {
"ES Math": [
{
"grade_level": "ES",
"subject": "Math",
"subject_abbr": "Math",
"product_id": 438,
"product_title": "Test1",
"ratings": [
{
"review_org": "Org1",
"review_url": "url",
"review_items": [
{
"name": "green_alignment_ratings",
"value": 14,
"label": "Meets Expectations",
"label_color": "Green"
},
{
"name": "green_usability_ratings",
"value": 34,
"label": "Green",
"label_color": 38
}
]
},
{
"review_org": "Org2",
"review_url": "url",
"review_items": [
{
"name": "Overall",
"value": null,
"Label": "Tier I, Exemplifies quality",
"scale": null
}
]
}
]
},
{
"grade_level": "ES",
"subject": "Math",
"subject_abbr": "Math",
"product_id": 2085,
"product_title": "Test2",
"ratings": [
{
"review_org": "Org1",
"review_url": "url",
"review_items": [
{
"name":"green_alignment_ratings",
"value": 14,
"label": "Meets Expectations",
"label_color": "Green"
},
{
"name":"green_usability_ratings",
"value": 34,
"label": "Meets Expectations",
"label_color": "Green"
}
]
},
{
"review_org": "Org2",
"review_url": "url",
"review_items": []
}
]
}
]
}
}
}
And I tried something with the serializer below but this is yeilding some different JSON only.
class CurriculumSerializer(ModelSerializer):
grade_level = serializers.CharField(source='grade_level_dim')
subject_abbr = serializers.CharField(source='subject_abbr_dim')
product_id = serializers.IntegerField(source='dim_id')
product_title = serializers.CharField(source='title_dim')
ratings = serializers.SerializerMethodField()
class Meta:
model = Table2
fields = [
'grade_level',
'subject',
'subject_abbr',
'product_id',
'product_title',
'ratings'
]
def get_ratings(self,obj):
queryset = Table2.objects.all()
c = queryset.filter(id=obj.id, title_dim = obj.title_dim, ratings_source__isnull=False).distinct('id',)
if c.exists():
serializer = RatingsSerializer(c, many=True)
return serializer.data
else:
data = 'null'
return data
class RatingsSerializer(ModelSerializer):
review_items = serializers.SerializerMethodField()
Louisiana_rating = serializers.SerializerMethodField()
class Meta:
model = Table3
fields = ['source','report_url','review_items','Louisiana_rating']
def get_review_items(self, obj):
queryset = Table3.objects.all()
c = queryset.filter(source__iexact = 'Org1', title = obj.title_dim, grade_level = obj.grade_level, subject_abbr = obj.subject_abbr_dim)
# print(c.query)
if c.exists():
serializer = reportSerializer(c, many=True)
return serializer.data
else:
data = 'null'
return data
def get_Louisiana_rating(self, obj):
queryset = Table3.objects.all()
c = queryset.filter(source__iexact = 'Org2', title = obj.title_dim, grade_level = obj.grade_level, subject_abbr = obj.subject_abbr_dim)
if c.exists():
serializer = reportSerializer(c, many=True)
return serializer.data
else:
data = 'null'
return data
class reportSerializer(ModelSerializer):
class Meta:
model = Table3
fields = ['name',
'value',
'label',
'label_color']
class DistrictDetailSerializer(ModelSerializer):
curriculum = serializers.SerializerMethodField()
class Meta:
model = Table1
exclude = ('finance_total', 'revenue_total')
def get_curriculum(self, obj):
queryset_list = Table2.objects.all()
c = queryset_list.filter(id=obj.id)\
.distinct('col1','col2')
if c.exists():
serializer = CurriculumSerializer(c, many=True)
curriculum_map = {}
for d in serializer.data:
key = f"{d['grade_level']} {d['subject_abbr']}"
if key in curriculum_map:
curriculum_map[key].append(d)
else:
curriculum_map[key] = [d, ]
return curriculum_map
else:
data = 'null'
return data
The table3 has all details for ratings like source, report_url, name,label,value,label_colour I want this values as show in the JSON below.
This error usually occurs if the user account was not given/assigned to the Virtual Machine User Login role on the VMs.
Please check this https://learn.microsoft.com/en-us/azure/active-directory/devices/howto-vm-sign-in-azure-ad-windows#azure-role-not-assigned to assign required roles and try login.

format the results in kusto

example
in the following example we need to summerize the user logs.
datatable(user:string, dt:datetime,page: string, value:int)
[
'chmod', datetime(2019-07-15), "page1", 1,
'ls', datetime(2019-07-02), "page2", 2,
'dir', datetime(2019-07-22), "page3", 3,
'mkdir', datetime(2019-07-14), "page4", 4,
'rm', datetime(2019-07-27), "page5", 5,
'pwd', datetime(2019-07-25), "page6", 6,
'rm', datetime(2019-07-23), "page7", 7,
'pwd', datetime(2019-07-25), "page8", 8,
]
| summarize commands_details = make_list(pack('dt', dt, 'page', page, "value", value)) by user
results
the results in the last example query will be like
"user": pwd,
"commands_details": [
{
"dt": "2019-07-25T00:00:00.0000000Z",
"page": "page6",
"value": 6
},
{
"dt": "2019-07-25T00:00:00.0000000Z",
"page": "page8",
"value": 8
}
],
expected results
but i need the results to be like the following
"user": pwd,
"commands_details": [
{
"dt": "2019-07-25T00:00:00.0000000Z",
"data":[
{"page": "page6", "value": 6},
{"page": "page8", "value": 8}
]
}
],
question
is there any way in Kusto to formate the results like the expected section?
You can use next query to achieve this:
datatable(user:string, dt:datetime,page: string, value:int)
[
'chmod', datetime(2019-07-15), "page1", 1,
'ls', datetime(2019-07-02), "page2", 2,
'dir', datetime(2019-07-22), "page3", 3,
'mkdir', datetime(2019-07-14), "page4", 4,
'rm', datetime(2019-07-27), "page5", 5,
'pwd', datetime(2019-07-25), "page6", 6,
'rm', datetime(2019-07-23), "page7", 7,
'pwd', datetime(2019-07-25), "page8", 8,
]
| summarize commands_details = make_list(pack('page', page, "value", value)) by user, dt
| project result = pack('user', user, 'data', pack('dt', dt, 'data', commands_details))
A result (for 'pwd'):
{
"user": "pwd",
"data": {
"dt": "2019-07-25T00:00:00Z",
"data": [
{
"page": "page6",
"value": 6
},
{
"page": "page8",
"value": 8
}
]
}
}