Skip to content

Added a helper to extract data from JSON structs inside data frames#54

Merged
kiddinn merged 2 commits into
masterfrom
new_frame_helper
Jan 31, 2022
Merged

Added a helper to extract data from JSON structs inside data frames#54
kiddinn merged 2 commits into
masterfrom
new_frame_helper

Conversation

@kiddinn

@kiddinn kiddinn commented Jan 31, 2022

Copy link
Copy Markdown
Contributor

This is a PR that adds a simple feature into the lw object to extract data from data frames that contain JSON structs.

Example use case:

df['os'] = lw.deep_extract_field(df, 'properties', 'user_agent.extracted_values.os')

Would extract the os field from a JSON struct that that would look like

{
  'user_agent': {
    'extracted_values': {
      'os': 'myhost_os'
    },
  },
}

@kiddinn kiddinn requested a review from alannix-lw January 31, 2022 13:06

@alannix-lw alannix-lw left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Comment on lines +47 to +54
for point in item.split("."):
if not isinstance(data, dict):
logger.error(
"Sub-item %s is not a dict (%s)", point, type(data))
return np.nan

data = data.get(point)
return data

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clever! 🧠

@kiddinn kiddinn merged commit 666049d into master Jan 31, 2022
@kiddinn kiddinn deleted the new_frame_helper branch January 31, 2022 15:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants