#How to get specific data from a part of a JSON file

33 messages · Page 1 of 1 (latest)

brittle shell
#

I'm trying to create a program that can calculate the required resources to get a specified amount of items per minute.
Here's an example of the json file

{
        "id": "advanced-circuit",
        "name": "Advanced circuit",
        "type": "Intermediate product",
        "wiki_link": "https://wiki.factorio.com/Advanced_circuit",
        "category": "Intermediate product",
        "recipe": {
            "time": 6,
            "yield": 1,
            "ingredients": [
                {
                    "id": "copper-cable",
                    "amount": 4
                },
                {
                    "id": "electronic-circuit",
                    "amount": 2
                },
                {
                    "id": "plastic-bar",
                    "amount": 2
                }
            ]
        }
    },
    {
        "id": "arithmetic-combinator",
        "name": "Arithmetic combinator",
        "type": "Logic",
        "wiki_link": "https://wiki.factorio.com/Arithmetic_combinator",
        "category": "Logistics",
        "recipe": {
            "time": 0.5,
            "yield": 1,
            "ingredients": [
                {
                    "id": "copper-cable",
                    "amount": 5
                },
                {
                    "id": "electronic-circuit",
                    "amount": 5
                }
            ]
        }
    },

There are hundreds of different items. What I'm trying to do is have the user input the name of an item and return the recipe for that item. So if they say "Advanced circuit", it returns 4 copper cables, 2 electronic circuits, and 2 plastic bars, as well as the time it takes to craft the item and the yield. I've been googling this, but I'm not really getting anywhere. How would I do this?

snow cypressBOT
#

This post has been reserved for your question.

Hey @brittle shell! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

snow mesa
#

Look into Jackson

brittle shell
#

I've already looked into Jackson. The problem is, all the tutorials are showing how to get specific fields from one object. I'm trying to search the whole file for a specific name, and then search that object that has that name for the recipe

#

I'm not sure how to use Jackson to find that

solemn lagoon
#

ahh the factory must growww

snow mesa
#

You'll have to parse the thing in order to search through it. Or you'll have to do it yourself and stream through the whole thing to search for what you hope is the correct node

solemn lagoon
#

couldnt you just use factory planner?

snow mesa
#

You've gotta index it anywat

brittle shell
brittle shell
snow mesa
#

You don't really want to do that 😶

brittle shell
#

._.

#

why not?

snow mesa
#

You've got a library that can stream all the nodes for you, all you have to do is extract the ID's and the recipes that go along with it

#

Same thing you'll have to do when you step through it by hand. But easier

solemn lagoon
snow mesa
#

Does org.json support stream?

#

Dunno how big the file is.

brittle shell
snow mesa
#

Oh, that's not that big

#

Well, get your library of choice and start parsing my man 🤣

brittle shell
#

dude my issue is that i don't know how to parse

#

that's what my question is lmao

nocturne rapids
#

The library knows

#

Jackson doesn't read one field, it transforms the JSON into objects

#

In both cases it's the same: make the library parse, explore all the items until you find the wanted name

snow mesa
#

You can use anything. Just read the documentation and read it

#

(the file)

solemn lagoon
#

linked article explains how to use those