locked
Extracting values from json RRS feed

  • Question

  • Hi All.

    I have output values similar to the below from a scriptblock.

    I would like to extract the value of step 2.

    Is there a specific way to do this as i am not sure about the formatting.

    The data type is json, I tried to convert this to a hash table but not sure if thats the best way, from what I see one should be able to convert from json back.

    {
        "Step1":  "Running step 1"
    }
    {
        "Step2":  "Running step 2."
    }
    {
        "Step3":  "Running step 3"
    }
    {
        "Step4":  "Running step 4"
    }
    

    Thanks in advance.

    Wednesday, March 4, 2020 5:17 PM

All replies

  • Normally I would say convertfrom-json, but powershell doesn't like it unless a whole object is on each line, or it was an array of objects.  This is unusual, since the property is different in each object.  Only step 2 has a period?


    EDIT:


    get-content file.json -ReadCount 3 | foreach { $_ | convertfrom-json } | fl


    Step1 : Running step 1

    Step2 : Running step 2.

    Step3 : Running step 3

    Step4 : Running step 4

    Wednesday, March 4, 2020 5:27 PM
  • Sorry they all had a period, how would one use convertfrom json ?

    should also be

    $val = {
        "Step1":  "Running step 1"
    }
    {
        "Step2":  "Running step 2."
    }
    {
        "Step3":  "Running step 3"
    }
    {
        "Step4":  "Running step 4"
    }

    • Edited by MrFlinstone Wednesday, March 4, 2020 6:53 PM
    Wednesday, March 4, 2020 6:49 PM
  • That causes a syntax error.  I've already shown a convertfrom-json example.  
    Thursday, March 5, 2020 3:05 PM