Splunk Search

Fix JSON format by replacing single quote and put into new field

LearningGuy
Builder

Hello,
| dbxquery connection=test query="select employee_data from company"
The following employee_data is not in proper JSON format, so I can't use spath.
How do I replace single quote (') with double quote ("), replace None with "None" and put it on a new field?
Thank you for your help.

employee_data
[{company':'company A','name': 'employee A1','position': None}, {company': 'company A','name': 'employee A2','position': None}]
[{company':'company B','name': 'employee B1','position': None}, {company': 'company B','name': 'employee B2','position': None}]
[{company':'company C','name': 'employee C1','position': None}, {company': 'company C','name': 'employee C2','position': None}]

 

Labels (1)
0 Karma
1 Solution

dtburrows3
Builder

More of just an additional screenshot for context and a fieldname with a description of the eval that was done to it... but I see that is has caused some confusion.

Here is full search of the local emulation.

| makeresults
    | eval
        employee_data="[{'company':'company A','name': 'employee A1','position': None}, {'company': 'company A','name': 'employee A2','position': None}]"
    | append
        [
            | makeresults
                | eval
                    employee_data="[{'company':'company B','name': 'employee B1','position': None}, {'company': 'company B','name': 'employee B2','position': None}]"
            ]
    | append
        [
            | makeresults
                | eval
                    employee_data="[{'company':'company C','name': 'employee C1','position': None}, {'company': 'company C','name': 'employee C2','position': None}]"
            ]
    | eval
        formatted_data=replace(replace(employee_data, "\'", "\""), "None", "\"None\"")
    | spath input=formatted_data

View solution in original post

LearningGuy
Builder

Hello,
Which part of your search that put the into a new field "formatted_data"?
I don't see "formatted_data" in the search.
Can you paste the whole search including how you put simulated data?

Thank you for your help.

Thanks

0 Karma

dtburrows3
Builder

More of just an additional screenshot for context and a fieldname with a description of the eval that was done to it... but I see that is has caused some confusion.

Here is full search of the local emulation.

| makeresults
    | eval
        employee_data="[{'company':'company A','name': 'employee A1','position': None}, {'company': 'company A','name': 'employee A2','position': None}]"
    | append
        [
            | makeresults
                | eval
                    employee_data="[{'company':'company B','name': 'employee B1','position': None}, {'company': 'company B','name': 'employee B2','position': None}]"
            ]
    | append
        [
            | makeresults
                | eval
                    employee_data="[{'company':'company C','name': 'employee C1','position': None}, {'company': 'company C','name': 'employee C2','position': None}]"
            ]
    | eval
        formatted_data=replace(replace(employee_data, "\'", "\""), "None", "\"None\"")
    | spath input=formatted_data

LearningGuy
Builder

Hello,

I tested your solution and it worked.    Thank you for your help. 
1) if I put eval on employee_data, will it change the original data?
2) I apply the solution on the real data and I got the following error. How do I fix this without increasing memory limit?
command.mvexpand: output will be truncated at 1000 results due to excessive memory usage. Memory threshold of 500MB as configured in limits.conf / [mvexpand] / max_mem_usage_mb has been reached

0 Karma

dural_yyz
Communicator

It sounds like the original data has too many lines/events.  You may want to implement EventBreaker or LineBreak in your props.conf on the ingest before you need to apply any search time eval or mvexpand.

0 Karma

dtburrows3
Builder

Maybe something like this?

 

 

<base_search>
    | eval
        employee_data=replace(replace(employee_data, "\'", "\""), "None", "\"None\"")
    | spath input=employee_data

 

 


Testing on my local instance looks like it worked out.

dtburrows3_0-1705532665943.png

 

dtburrows3_2-1705532793823.png

 

 

 

Get Updates on the Splunk Community!

More Ways To Control Your Costs With Archived Metrics | Register for Tech Talk

Tuesday, May 14, 2024  |  11AM PT / 2PM ET Register to Attend Join us for this Tech Talk and learn how to ...

.conf24 | Personalize your .conf experience with Learning Paths!

Personalize your .conf24 Experience Learning paths allow you to level up your skill sets and dive deeper ...

Threat Hunting Unlocked: How to Uplevel Your Threat Hunting With the PEAK Framework ...

WATCH NOWAs AI starts tackling low level alerts, it's more critical than ever to uplevel your threat hunting ...