Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there a way to get just the string value inside json when doing stringify? #254

Open
nooblyf opened this issue Aug 5, 2023 · 2 comments

Comments

@nooblyf
Copy link

nooblyf commented Aug 5, 2023

when i stringify anything i get json and meta (or sometimes just json) like this

stringify({
  flasjhdfojha: 45234524,
  fadsfasd: { fasfd: { dfasdfasdf: { fasdfasd: 341234123, date: new Date() } } },
})
// {"json":{"flasjhdfojha":45234524,"fadsfasd":{"fasfd":{"dfasdfasdf":{"fasdfasd":341234123,"date":"2023-08-05T19:39:45.674Z"}}}},"meta":{"values":{"fadsfasd.fasfd.dfasdfasdf.date":["Date"]}}}

stringify("helooooo")
// {"json":"helooooo"}

is there a way to get just the value inside json as a string? something like

// string
// {"flasjhdfojha":45234524,"fadsfasd":{"fasfd":{"dfasdfasdf":{"fasdfasd":341234123,"date":"2023-08-05T19:39:45.674Z"}}}}

// string
// helooooo

what i'm currently doing is using regex to remove json (key) and meta completely (if it exists)

stringify(data).replace(/^(({\"json\":?)(\"?))|(,"meta":.*)|((\"?)(}?$))/gi, '');
@nooblyf nooblyf changed the title Is there a way to get just the string value inside json? Is there a way to get just the string value inside json using stringify? Aug 5, 2023
@nooblyf nooblyf changed the title Is there a way to get just the string value inside json using stringify? Is there a way to get just the string value inside json when doing stringify? Aug 5, 2023
@Skn0tt
Copy link
Member

Skn0tt commented Aug 7, 2023

Hi @nooblyf! You could use SuperJSON.serialize, grab the json key and stringify that using JSON.stringify. That means all type information will be lost, though, losing the main benefit of SuperJSON. Depending on what you're looking for, JSON's replacer option might be a better fit.

@nooblyf
Copy link
Author

nooblyf commented Aug 7, 2023

I wanted this for logging purposes only, i guess JSON.stringify is the way to go

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

No branches or pull requests

2 participants