1

I have developed a powershell script which generates a JSON file which am trying to parse using JQuery.

var mydata = jQuery.parseJSON(w3wp);

 w3wp = '[{"Pid" : "6724" , "Apppool" : "myapppool1" , "Usage" : "229.328125"},{"Pid" : "6808" , "Apppool" : "myapppool2" , "Usage" : "152" }]';

There are no issues found when the JSON data is in a single line like above, the script works fine. If the json data is formatted like below i am getting "undefined error in w3wp."

 w3wp = '[{"Pid" : "6724" , "Apppool" : "myapppool1" , "Usage" : "229.328125"},
 {"Pid" : "6808" , "Apppool" : "myapppool2" , "Usage" : "152" }]';

The representation above is with very minimal data in real time scenario the number of app pools are huge and power-shell word wraps in 2.0 and am having issues with the JSON file. Is there any way to format this Json file so i can use this for parsing ?

Based on request here is the powershell script

$host.UI.RawUI.BufferSize = new-object System.Management.Automation.Host.Size 5512,100
function get-apppool {
    [regex]$pattern="-ap ""(.+)"""
    $contentx="w3wp = '["
    gwmi win32_process -filter 'name="w3wp.exe"' | % {
        $name=$_.name
        $cmd = $pattern.Match($_.commandline).Groups[1].Value
        $procid = $_.ProcessId
        $cpuper = get-process | where-object {$_.id -like $procid} | select -Expand CPU
        $contentx = $contentx + "{""Pid"" : ""$procid"" , ""Apppool"" : ""$cmd"" , ""Usage"" : ""$cpuper"" }"
        $contentx = $contentx + ","
    }
    $contentx = $contentx -replace ".$"
    $contentx = $contentx + "]';"
    write-host -nonewline $contentx > dsdf.json 
} 
get-apppool
Keith Hill
  • 194,368
  • 42
  • 353
  • 369
Sudheej
  • 1,873
  • 6
  • 30
  • 57
  • it it the carriage return that is the problem? – PlantTheIdea Jul 07 '14 at 21:55
  • I am not sure, i am looking for a way to split these into multiple lines – Sudheej Jul 07 '14 at 21:57
  • Well,. am unable to print a single line in powershell, despite using System.Management.Automation.Host.Size the value still warps and the output file has multiple lines which am unable to parse,. if there is a way to split these then i would implement this logic in powershell to format the JSON file. – Sudheej Jul 07 '14 at 22:03
  • have you tried just using the native javascript function `JSON.parse(w3wp)`? working as far as i can see => http://jsfiddle.net/qLeJf/ – PlantTheIdea Jul 07 '14 at 22:09
  • Thanks, i just tried that same thing !!.. it works with a single line but throws error with new line. – Sudheej Jul 07 '14 at 22:13
  • why not strip the newline from the string then? i hate doing this, but `JSON.parse(w3wp.replace(/(\r\n|\n|\r)/gm,""))` ... this regex strips all three types of traditional line breaks – PlantTheIdea Jul 07 '14 at 22:15
  • doesnt help :( here is the section from my script http://jsfiddle.net/y5V6R/ – Sudheej Jul 07 '14 at 22:23
  • aside from jQuery not being called, w3wp is not defined anywhere in that script? – PlantTheIdea Jul 07 '14 at 22:29
  • note w3wp is inside the json file dfdsf.json!! – Sudheej Jul 07 '14 at 22:33
  • 1
    your code there makes no sense ... nested `script` tags? and referencing a JSON file via script tag titled "text/javascript" is bad form. it should be ` – PlantTheIdea Jul 07 '14 at 22:38
  • Point taken,. ive made changes but still it doesnt work here is my script section http://jsfiddle.net/y5V6R/2/ – Sudheej Jul 07 '14 at 22:44
  • yeah i mean u keep setting up jsFiddles that dont work because they are poorly structured, but more importantly do not have the `dfdsf.json` file uploaded. i cant help fix what i cant touch u know. – PlantTheIdea Jul 07 '14 at 22:48
  • Well the point is my app works!! the only concern here is with formatting the json file. My json file is nothing more but a single line which is part of the question i posted. Note : Json file is local – Sudheej Jul 07 '14 at 22:52
  • I am a powershell developer fiddling with jscript so sorry about the cosmetic part of jscript., i played rough.. ., thanks!!! i will look for answers from other users. – Sudheej Jul 07 '14 at 23:01
  • If your issue is how PowerShell is outputting your file, perhaps you should show the relevant code from PowerShell that produces the file so we can look at that end of things. – TheMadTechnician Jul 07 '14 at 23:02
  • @TheMadTechnician I added the script to the question. – Sudheej Jul 07 '14 at 23:10
  • 1
    It's quittin' time for me, so I'm going home, but I'll get back to you about that PowerShell script part if nobody else has in the morning. – TheMadTechnician Jul 07 '14 at 23:18
  • Are you getting any output from your shellscript at all? If so, please add it to your post. – CMPSoares Jul 08 '14 at 00:46

3 Answers3

3

You don't have to construct the Json yourself, let PowerShell do that for you - at least in V3 and higher which has the handy ConvertTo-Json command e.g.:

Get-WmiObject Win32_Process -Filter 'name="w3wp.exe"' | 
    Where {$_.commandline -match '-ap "(.*)"'} | 
    Select @{n='Pid';e={$_.ProcessId}},
           @{n='AppPool';e={$_.matches[1]}},
           @{n='Usage';e={[float](Get-Process -id $_.ProcessId).CPU}} | 
    ConvertTo-Json > dsdf.json
Keith Hill
  • 194,368
  • 42
  • 353
  • 369
1

Supposing you're not using V3 (you said you're using 2.0), I can suggest the following (I'm not able to confirm this because you haven't posted the script's output):

Backticks:

If you want to add Single and Double Quotes in a string I suggest you always use backticks, for example:

$contentx="w3wp = `'["

Bad formatting in JSON:

You're adding a comma (,) after every array object, if you don't remove this one it will generate an undefined error in w3wp, you can correct this by removing it's last comma before finishing the JSON string like this:

$contentx = $contentx.Substring(0,$contentx.Lenght-1);
$contentx = $contentx + "]`';"

Write-host isn't doing what you expect:

Well and as described in CB's answer:

Write-host redirect the output only to the console.

You can use write-output $contentx > dsdf.json instead.

Other possible errors:

You can have also problems in the way you load the data from file, but I don't have enough information to evaluate that right now.

Community
  • 1
  • 1
CMPSoares
  • 4,175
  • 3
  • 24
  • 42
0

JSON.Stringfy fixed the problem for me, now i am able to parse the string properly from the JSON file. The issue was due to the single quote at the begining and the end which caused issues with JSON parse. I ran JSON.Stringfy and then pass that to JSON.Parse which fixed the issue. Thanks for your comments and suggestions.

Sudheej
  • 1,873
  • 6
  • 30
  • 57