Search code examples
jsonfor-loopbatch-filefind

Get a certain string before the specified string and after the second specified string [BATCH]


I'm making a Batch script to get the actual stock of an product on a page, The JSON is :

... ,"src":"https://www.datocms-assets.com/34299/1615563408-h510-elite-white-black-fan-bracket-out.png?ar64=MTox&auto=format&fit=crop&h=375&w=375","width":375,"height":375,"alt":null,"title":null}}],"aliasImage":null,"price":179.99,"stock":0,"id":260,"shopify_variants_id":39412773781641,"shopify_parent_id":6596349198473,"isValid":true,"isLowStock":true,"isOutOfStock":true,"discounted_price":124.99,"isOnSale":true,"isComingSoon":false,"isNewItem":false,"showSaleTag":false,"isPromo":true,"showCountdownTimer":false,"countdownEndDate":null,"showPercentOff":true,"percentOff":30,"showInventoryCount":false,"showAmountOff":false,"amountOff":0,"isBestSeller":false} ...

I need to get the stock, the and the dismounted price, if someone would be able to help me make this, i would appreciate it. I precise that they are multiples : "discounted_price" and "price" on the json, it contains all the nzxt product

i've thinked about getting the string before : "id":260 but found nothing working correctly. i dont need exactly this idea, but i need: something that work! thanks you ^^


Solution

    • This is not a code-writting service. You should write your own code and we give help with it.
    • A Batch file is not the right tool to process JSON data.
    • We need an actual data in order to do tests on it.

    Anyway, this is a small Batch code that may help:

    @echo off
    setlocal
    
    set json={"src":"https://www.datocms-assets.com/34299/1615563408-h510-elite-white-black-fan-bracket-out.png?ar64=MTox&auto=format&fit=crop&h=375&w=375","width":375,"height":375,"alt":null,"title":null}}],"aliasImage":null,"price":179.99,"stock":0,"id":260,"shopify_variants_id":39412773781641,"shopify_parent_id":6596349198473,"isValid":true,"isLowStock":true,"isOutOfStock":true,"discounted_price":124.99,"isOnSale":true,"isComingSoon":false,"isNewItem":false,"showSaleTag":false,"isPromo":true,"showCountdownTimer":false,"countdownEndDate":null,"showPercentOff":true,"percentOff":30,"showInventoryCount":false,"showAmountOff":false,"amountOff":0,"isBestSeller":false}
    
    rem Remove from beginning up to "width"
    set "json=%json:*width="width%"
    
    rem Change quote-colon by equal-sign
    set "json=%json:":==%"
    
    rem Separate comma-delimited parts and assign them to individual variables
    set %json:,=" & set %
    
    echo stock=%stock%
    echo discounted_price=%discounted_price%
    

    The output of this code is:

    stock=0
    discounted_price=124.99
    

    Please, do not post a comment indicating that this code does not work with your actual data! :( Instead, remove the @echo off line, run the program, comprehend how it works and insert the needed modifications until the program correctly work...