-
Notifications
You must be signed in to change notification settings - Fork 21
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
Search file for ID and return full value of matching objects #9
Comments
JsonReader works in a forward-only manner, so if you might need prior data you'll need to hang onto it until that determination can be made. The easiest way to do this would be to step into the array, grab each object in full, check the parentCategory and ignore any that don't match. E.g.:
Note that because numbers get returned as strings (this will likely become optional in a future version) and you didn't get the opportunity to inspect their type, you'll lose their type information this way. If you need to retain that and you know ahead of time what should be a number it's easy to fix them up:
(The unary Let me know if this doesn't work out for whatever reason. There is always another way to do things, it just might be a bit more cumbersome. |
Wow, that's exactly what I was looking for! And yes, I will probably know before what will be numbers so I should be able to fix things up :) Thank you so much for your help and for your great work, such a versatile tool! |
Hi again @pcrov , I'm importing a rather large json file and I'm trying to stream it from the API server using fopen. I'm having a bit of a problem making it efficient though, it feels like the parsing takes a really long time. Right now my code looks like this:
As you can see, I've added the line:
in an attempt to free memory, but not sure if it has any effect. Does this look like a good solution to you? Thanks! // Jens. |
If you haven't already done so upgrade to the latest release, 0.7.0, as it's significantly faster than prior versions. There are still more speed improvements in the works, but nothing quite like the jump 0.7.0 made. Make sure xdebug isn't loaded at all. Even when not enabled the extension has a massive performance impact. Parsing a stream from a remote API directly while supported won't be as quick as parsing a local file, though from the code you've posted it looks like you're dealing with a local file already. I wouldn't expect At the end of the day parsing massive files in PHP can only be so fast, and the low memory consumption you get from a streaming parser will always come at the expense of speed. It's the kind of thing best suited to running in a background task and checking the result later. |
Hi there and first of all, thank you for this amazing parser. A true life saver.
I'm currently trying to fully understand how it works, but have run into a problem that I can't really figure out how to solve.
I have a large Json file that looks like this (part of it):
What I'm trying to do is search through this file after all instances where "parentCategory" equals 2570 and then print/echo the whole object that this ID is part of.
So far, this is what I've got:
This prints the parentCategory ID, but what I need is to be able to use the parentCategory name and value to identify the whole object it belongs to and in the end return the following:
Is this achievable with your parser?
Thank you so much for any help you can give me!
The text was updated successfully, but these errors were encountered: