admin管理员组文章数量:1418024
I'm currently working on a Splunk query and need some assistance with converting nested JSON data into a specific string format. Below is an example of the JSON data I'm dealing with:
{
"info1": "val1",
"info2": "val2",
"UsagePerUser": {
"user1": 0.1,
"user2": 0.2
}
}
I want to convert the UsagePerUser field into a string with the following format:
Current Output:
info1 info2 UsagePerUser
===========================
val1 val2 {"user1":0.1,"user2":0.2}
---------------------------
val3 val4 {"user3":0.3,"user4":0.4}
Desired Output:
info1 info2 UsagePerUser
===========================
val1 val2 user1 10.0
user2 20.0
---------------------------
val3 val4 user3 30.0
user4 40.0
Here's the Splunk command I'm currently using:
index="my_index" source="my_source"
| sort - _time
| dedup Path
| spath output=UsagePerUser path=UsagePerUser
| eval UsagePerUser = json(UsagePerUser)
| table info1, info2, UsagePerUser
This command only extracts the UsagePerUser sub-dictionary, but I need to transform it into the desired string format. Any suggestions or guidance on how to modify this command to achieve the desired output would be greatly appreciated
I'm currently working on a Splunk query and need some assistance with converting nested JSON data into a specific string format. Below is an example of the JSON data I'm dealing with:
{
"info1": "val1",
"info2": "val2",
"UsagePerUser": {
"user1": 0.1,
"user2": 0.2
}
}
I want to convert the UsagePerUser field into a string with the following format:
Current Output:
info1 info2 UsagePerUser
===========================
val1 val2 {"user1":0.1,"user2":0.2}
---------------------------
val3 val4 {"user3":0.3,"user4":0.4}
Desired Output:
info1 info2 UsagePerUser
===========================
val1 val2 user1 10.0
user2 20.0
---------------------------
val3 val4 user3 30.0
user4 40.0
Here's the Splunk command I'm currently using:
index="my_index" source="my_source"
| sort - _time
| dedup Path
| spath output=UsagePerUser path=UsagePerUser
| eval UsagePerUser = json(UsagePerUser)
| table info1, info2, UsagePerUser
This command only extracts the UsagePerUser sub-dictionary, but I need to transform it into the desired string format. Any suggestions or guidance on how to modify this command to achieve the desired output would be greatly appreciated
Share Improve this question asked Jan 29 at 19:07 JanetJanet 12 bronze badges1 Answer
Reset to default 0Try this run-anywhere SPL; find the explanation in the code:
| makeresults ```start mock data```
format=json
data="
[
{
\"info1\": \"val1\",
\"info2\": \"val2\",
\"UsagePerUser\": {
\"user1\": 0.1,
\"user2\": 0.2
}
},
{
\"info1\": \"val1\",
\"info2\": \"val2\",
\"UsagePerUser\": {
\"user3\": 0.3,
\"user4\": 0.4
}
}
]
"
```start your query```
| spath output=UsagePerUser path=UsagePerUser
| eval UsagePerUser = json(UsagePerUser)
| table info1, info2, UsagePerUser
```end your query```
```extract usernames (un) and usage from json into multivalue fields```
| rex field=UsagePerUser max_match=100 "\"(?P<un>[^\"]+)\":(?P<usage>[\d.]+)"
| eval
````fix usage magnitude```
usage=mvmap(usage,usage*100),
```zip together usernaem and usage```
UsagePerUser=mvzip(un,usage," ")
| fields - un usage
本文标签: Converting Nested JSON to String in SplunkStack Overflow
版权声明:本文标题:Converting Nested JSON to String in Splunk - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745282600a2651510.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论