Splunk Search

json field extraction

Nadhiya_Dubai
Explorer

Hi ,

Below is the json snippet

properties: { [-]
columns: [ [-]
{ [-]
name: PreTaxCost
type: Number
}
{ [-]
name: UsageDate
type: Number
}
{ [-]
name: Currency
type: String
}
]
nextLink: null
rows: [ [-]
[ [-]
37.399436789282746
20200301
USD
]
[ [-]
37.4605201027181
20200302
USD
]

how can i extract the fields pretaxcost ,usagedate

Labels (1)
Tags (1)
0 Karma
1 Solution

manjunathmeti
Champion

Hi @Nadhiya_Dubai,

Try this:

| spath path=properties.rows{}{} output=values | nomv values | eval values=split(values, "USD") | mvexpand values | rex field=values "^\s*(?<PreTaxCost>[\d\.]+)\s*(?<UsageDate>[\d]+)$" | where isnotnull(PreTaxCost) and isnotnull(UsageDate) | spath | table id, name, eTag, name, type, PreTaxCost, UsageDate, property.nextLink

Sample query:

| makeresults | eval _raw="{\"sku\": null, \"name\": \"b0b36773-618e-4490-9c11-57eaf5cb0f49\", \"properties\": {\"rows\": [[37.39104138816985, 20200301, \"USD\"], [37.45212432304218, 20200302, \"USD\"], [37.19654530403741, 20200303, \"USD\"], [37.34428721156659, 20200304, \"USD\"], [38.220217017864336, 20200305, \"USD\"], [37.70977490679157, 20200306, \"USD\"], [37.53643148801985, 20200307, \"USD\"], [37.47332013352408, 20200308, \"USD\"], [27.71226225182299, 20200309, \"USD\"]], \"columns\": [{\"type\": \"Number\", \"name\": \"PreTaxCost\"}, {\"type\": \"Number\", \"name\": \"UsageDate\"}, {\"type\": \"String\", \"name\": \"Currency\"}], \"nextLink\": null}, \"eTag\": null, \"location\": null, \"type\": \"Microsoft.CostManagement/query\", \"id\": \"subscriptions/2808ca16-1c43-4d1c-8088-223b7c378d46/providers/Microsoft.CostManagement/query/b0b36773-618e-4490-9c11-57eaf5cb0f49\"}" | spath path=properties.rows{}{} output=values | nomv values | eval values=split(values, "USD") | mvexpand values | rex field=values "^\s*(?<PreTaxCost>[\d\.]+)\s*(?<UsageDate>[\d]+)$" | where isnotnull(PreTaxCost) and isnotnull(UsageDate) | spath | table id, name, eTag, name, type, PreTaxCost, UsageDate, property.nextLink

View solution in original post

0 Karma

manjunathmeti
Champion

Hi @Nadhiya_Dubai,

Try this:

| spath path=properties.rows{}{} output=values | nomv values | eval values=split(values, "USD") | mvexpand values | rex field=values "^\s*(?<PreTaxCost>[\d\.]+)\s*(?<UsageDate>[\d]+)$" | where isnotnull(PreTaxCost) and isnotnull(UsageDate) | spath | table id, name, eTag, name, type, PreTaxCost, UsageDate, property.nextLink

Sample query:

| makeresults | eval _raw="{\"sku\": null, \"name\": \"b0b36773-618e-4490-9c11-57eaf5cb0f49\", \"properties\": {\"rows\": [[37.39104138816985, 20200301, \"USD\"], [37.45212432304218, 20200302, \"USD\"], [37.19654530403741, 20200303, \"USD\"], [37.34428721156659, 20200304, \"USD\"], [38.220217017864336, 20200305, \"USD\"], [37.70977490679157, 20200306, \"USD\"], [37.53643148801985, 20200307, \"USD\"], [37.47332013352408, 20200308, \"USD\"], [27.71226225182299, 20200309, \"USD\"]], \"columns\": [{\"type\": \"Number\", \"name\": \"PreTaxCost\"}, {\"type\": \"Number\", \"name\": \"UsageDate\"}, {\"type\": \"String\", \"name\": \"Currency\"}], \"nextLink\": null}, \"eTag\": null, \"location\": null, \"type\": \"Microsoft.CostManagement/query\", \"id\": \"subscriptions/2808ca16-1c43-4d1c-8088-223b7c378d46/providers/Microsoft.CostManagement/query/b0b36773-618e-4490-9c11-57eaf5cb0f49\"}" | spath path=properties.rows{}{} output=values | nomv values | eval values=split(values, "USD") | mvexpand values | rex field=values "^\s*(?<PreTaxCost>[\d\.]+)\s*(?<UsageDate>[\d]+)$" | where isnotnull(PreTaxCost) and isnotnull(UsageDate) | spath | table id, name, eTag, name, type, PreTaxCost, UsageDate, property.nextLink
0 Karma

nwenzl_splunk
Splunk Employee
Splunk Employee
0 Karma

to4kawa
Ultra Champion
| makeresults 
| eval _raw="properties: { 
columns: 
{ 
name: PreTaxCost
type: Number
}
{ 
name: UsageDate
type: Number
}
{ 
name: Currency
type: String
}
]
nextLink: null
rows: 
[
37.399436789282746
20200301
USD
]
[
37.4605201027181
20200302
USD
]"
| rex max_match=0 "(?ms)(?<pretaxcost>[\d\.]+)\s*(?<usagedate>\d+)"
0 Karma

Nadhiya_Dubai
Explorer

Hi .. Its not working .

0 Karma

to4kawa
Ultra Champion

what's your query?
so, your json log is not valied json.
what' s this log?

0 Karma

Nadhiya_Dubai
Explorer

When i give the above mentioned query provide by you with raw data ,it is working .
For my below json data

{ [-]
eTag: null
id: subscriptions/2808ca16-1c43-4d1c-8088-223b7c378d46/providers/Microsoft.CostManagement/query/b0b36773-618e-4490-9c11-57eaf5cb0f49
location: null
name: b0b36773-618e-4490-9c11-57eaf5cb0f49
properties: { [-]
columns: [ [-]
{ [-]
name: PreTaxCost
type: Number
}
{ [-]
name: UsageDate
type: Number
}
{ [-]
name: Currency
type: String
}
]
nextLink: null
rows: [ [-]
[ [-]
37.39104138816985
20200301
USD
]
[ [-]
37.45212432304218
20200302
USD
]

index=azure_new i | rex max_match=0 "(?ms)(?[\d.]+)\s*(?\d+)" | table pretaxcost,usagedate

It is not working . What is the issue

0 Karma

to4kawa
Ultra Champion

will you provide _raw data?

0 Karma

Nadhiya_Dubai
Explorer

{"sku": null, "name": "b0b36773-618e-4490-9c11-57eaf5cb0f49", "properties": {"rows": [[37.39104138816985, 20200301, "USD"], [37.45212432304218, 20200302, "USD"], [37.19654530403741, 20200303, "USD"], [37.34428721156659, 20200304, "USD"], [38.220217017864336, 20200305, "USD"], [37.70977490679157, 20200306, "USD"], [37.53643148801985, 20200307, "USD"], [37.47332013352408, 20200308, "USD"], [27.71226225182299, 20200309, "USD"]], "columns": [{"type": "Number", "name": "PreTaxCost"}, {"type": "Number", "name": "UsageDate"}, {"type": "String", "name": "Currency"}], "nextLink": null}, "eTag": null, "location": null, "type": "Microsoft.CostManagement/query", "id": "subscriptions/2808ca16-1c43-4d1c-8088-223b7c378d46/providers/Microsoft.CostManagement/query/b0b36773-618e-4490-9c11-57eaf5cb0f49"}

0 Karma
Get Updates on the Splunk Community!

Extending Observability Content to Splunk Cloud

Watch Now!   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to leverage ...

More Control Over Your Monitoring Costs with Archived Metrics!

What if there was a way you could keep all the metrics data you need while saving on storage costs?This is now ...

New in Observability Cloud - Explicit Bucket Histograms

Splunk introduces native support for histograms as a metric data type within Observability Cloud with Explicit ...