How to get the first json code between a text including 3 json codes

Can anyone help me with a regex to get the first json code inside a text that contains 1 or more codes?

I’m trying to use the following code: {([^’]+)}
But this get all the jsons inside the code

All files that I want to get the first code in json have one or more blocks of code, and I need to get only the first one, which is the code after the word "Item Definition"

Text with 2 more jsons:

ItemDefinition
{ // I'm trying to get the code from here  
    "itemid": 590532217,
    "shortname": "ammo.nailgun.nails",
    "displayName": {
        "token": "ammo.nailgun.nails",
        "english": "Nailgun Nails"
    },
    "displayDescription": {
        "token": "ammo.nailgun.nails.desc",
        "english": "Standard nailgun ammunition"
    },
    "iconSprite": {
        "instanceID": 148798
    },
    "category": 8,
    "selectionPanel": 0,
    "maxDraggable": 0,
    "itemType": 1,
    "amountType": 0,
    "occupySlots": 0,
    "stackable": 64,
    "quickDespawn": false,
    "rarity": 0,
    "spawnAsBlueprint": false,
    "inventorySelectSound": {
        "instanceID": 115050
    },
    "inventoryGrabSound": {
        "instanceID": 115050
    },
    "inventoryDropSound": {
        "instanceID": 81228
    },
    "physImpactSoundDef": {
        "instanceID": 60196
    },
    "condition": {
        "enabled": false,
        "max": 0.0,
        "repairable": false,
        "maintainMaxCondition": false,
        "foundCondition": {
            "fractionMin": 1.0,
            "fractionMax": 1.0
        }
    },
    "hidden": false,
    "flags": 0,
    "steamItem": {
        "instanceID": 0
    },
    "Parent": {
        "instanceID": 0
    },
    "worldModelPrefab": {
        "guid": "d8c823436ceda0f42a4da54a972807bf"
    },
    "Traits": 8,
    "panel": {
        "instanceID": 0
    }
} // Until here

ItemModProjectile
{
    ...other json code
}

1 thought on “How to get the first json code between a text including 3 json codes”

  1. You can use this function to extract the first JSON block, and parse it:

    function parseFirstJson(text) {
        try {
            const regex = /^[^\{]*([\s\S]*?[\r\n]\})[\s\S]*$/;
            let obj = JSON.parse(text.replace(regex, '$1'));
            return obj;
        } catch(e) {
            // return JSON parse error
            let obj = { error: e.toString() }; 
            return obj;
        }
    }
    
    const text =`ItemDefinition
    {
        "itemid": 590532217,
        "shortname": "ammo.nailgun.nails",
        "displayName": {
            "token": "ammo.nailgun.nails",
            "english": "Nailgun Nails"
        }
    }
    
    ItemModProjectile
    {
        "more": "stuff"
    }`;
    
    let obj = parseFirstJson(text);
    console.log(JSON.stringify(obj, null, ' '));

    Output:

    {
     "itemid": 590532217,
     "shortname": "ammo.nailgun.nails",
     "displayName": {
      "token": "ammo.nailgun.nails",
      "english": "Nailgun Nails"
     }
    }
    

    Explanation:

    • ^ – start of string
    • [^\{]* – scan until just before the first {
    • ( – start of capture group
      • [\s\S]*? – scan cautiously over all chars until:
      • [\r\n]\} – a } at the beginning of line
    • ) – end of capture group
    • [\s\S]*$ – scan over everything else until end of line
    Reply

Leave a Comment