Coder Social home page Coder Social logo

Comments (6)

pkoppstein avatar pkoppstein commented on June 5, 2024

The jm script (https://github.com/pkoppstein/jm) is based on JSON Machine, and could be used as follows to find the value of a specific key:

$ jm -s | grep --max-count 1 '^{"PTHT0000001"'

Even if you don't want to use jm itself, you could examine it to see how it accomplishes what you want.

Alternatively, you might consider using the --stream option of jq (https://github.com/stedolan/jq), which is designed for just this kind of problem:

$ jq -n --stream 'first(fromstream( (inputs | select(.[0][0] == "PTHT0000001")), [["PTHT0000001"]]  ))'

from json-machine.

halaxa avatar halaxa commented on June 5, 2024

If you need to use it from inside PHP, just use simple foreach and find your key there.

foreach (Items::fromFile('500gb.json') as $key => $item) {
    if ($key === "PTHT0012803") {
        // your code
    }
}

Keep in mind, that a file of this size might get hours to parse with JSON Machine. I guess 2-4 depending on the machine and PHP configuration. You also might be interested in #97.

from json-machine.

halaxa avatar halaxa commented on June 5, 2024

Sorry, I read 500 GB instead of just 5 GB. Then it should be a matter of minutes. Make sure xdebug is disabled and JIT enabled.

Also make longer your php time limit if you parse from browser.

from json-machine.

KKKKK-tech avatar KKKKK-tech commented on June 5, 2024

The jm script (https://github.com/pkoppstein/jm) is based on JSON Machine, and could be used as follows to find the value of a specific key:

$ jm -s | grep --max-count 1 '^{"PTHT0000001"'

Even if you don't want to use jm itself, you could examine it to see how it accomplishes what you want.

Alternatively, you might consider using the --stream option of jq (https://github.com/stedolan/jq), which is designed for just this kind of problem:

$ jq -n --stream 'first(fromstream( (inputs | select(.[0][0] == "PTHT0000001")), [["PTHT0000001"]]  ))'

Thank you for your help! I will try the method you mentioned later.

from json-machine.

KKKKK-tech avatar KKKKK-tech commented on June 5, 2024

If you need to use it from inside PHP, just use simple foreach and find your key there.

foreach (Items::fromFile('500gb.json') as $key => $item) {
    if ($key === "PTHT0012803") {
        // your code
    }
}

Keep in mind, that a file of this size might get hours to parse with JSON Machine. I guess 2-4 depending on the machine and PHP configuration. You also might be interested in #97.

Thank you for your reply. I used code like this before, but it took too long. I think it may be because foreach takes too much time. Is there any way to avoid this situation in jsonmachine? If not, I will try to split large files.

from json-machine.

halaxa avatar halaxa commented on June 5, 2024

If you split them, it will take about the same time anyway. There is no faster solution in JSON Machine for now. Keep up with #97 which should bring some speedup.

from json-machine.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.