Extract data from json file throush "powershell" command - json

I am trying to get data from my json file but my code is still not working. Do you have any suggestion ?
JSON file :
"nodes": [
{
"id": "elfe",
"apps": [
{
"id": "man1",
"age" = "5"
"power" ="strenght"
},
{
"id": "man2",
"age" = "10"
"power" ="strenght"
}],
"id": "monster",
"apps": [
{
"id": "man3",
"age" = "5"
"power" ="strenght"
},
{
"id": "man4",
"age" = "10"
"power" ="strenght"
}],
And there, my code in PowerShell. I just want to get man1, man2, man3, man4 values in my file for each id elfe and monster like that :
man1
man2
in one file and in the other file:
man3
man4
My batch script :
Powershell -Nop -C "(Get-Content .\config.json |ConvertFrom-Json).Nodes | Select-Object -ExpandProperty id | Where-Object id -eq elfe" >> file.txt
EDIT : I can't modify my JSON file...

Your json is not valid, below is corrected version with data extraction:
$json = #"
{
"nodes":[
{
"id":"elfe",
"apps":[
{
"id":"man1",
"age":5,
"power":"strenght"
},
{
"id":"man2",
"age":10,
"power":"strenght"
}]},
{ "id":"monster",
"apps":[
{
"id":"man3",
"age" :"5",
"power":"strenght"
},
{
"id":"man4",
"age":"10",
"power":"strenght"
}]}
]}
"#
$data = $json | ConvertFrom-Json
$data.nodes | where {$_.id -eq "elfe"} | foreach {$_.apps.id >> "elfe.txt"}
But probably you just had troubles with quoting that command. Below is a working version. I replaced >> with tee - it will overwrite output file and also prints results on the screen.
powershell -nop -c "(cat test.json | ConvertFrom-Json).nodes | where {$_.id -eq 'elfe'} | foreach {$_.apps.id} | tee out.txt"

Related

Convert Json to CSV by jq filter

I have a json file with this content and want to convert it to CSV like below:
{
"fields": [
{
"id": 17,
"name": "Business Division",
"values": [
{
"id": 131,
"name": "Accounting",
"industry": [
"Accounting"
]
}
]
},
{
"id": 16,
"name": "Cancellation Reason",
"values": [
{
"id": 114,
"name": "Forgot"
}
]
}
]
}
CSV File format:
17,Business Division,131,Accounting,Accounting
16,Cancellation Reason,114,Forgot
I ran this command on the terminal:
jq -M -r -f industry.jq source.json |tr -d '"' >source.csv
this is the content of industry.jq file that is used as the filter:
.fields[]
| .values[] as $e
| $e.industry[]? as $s
| [.id, .name, $e.id, $e.name, $s? ]
| #csv
As result, the second line of the CSV file did not print
I think it's because of the .industry[] object that did not available in the second object in my Json
How can I print the above json in the needed format?
.fields[] | [ .id, .name, .values[].id, .values[].name, .values[].industry[]? ] | #csv
Will produce
17,"Business Division",131,"Accounting","Accounting"
16,"Cancellation Reason",114,"Forgot"
When invoked like:
jq --raw-output '.fields[] | [ .id, .name, .values[].id, .values[].name, .values[].industry[]? ] | #csv'
Multiple industries will be added behind
Try it online
I'd use try ... catch ... to make the value of the default explicit, e.g.
(try $e.industry[] catch null) as $s

Removing Objects from JSON using powershell

I have a test.json file which has the below structure:
{
"name": "test",
"class": "4",
"exam": "test",
"marks": "4"
}
I want to remove some pairs from it like exam and class, and ultimately it should look like below:
{
"name": "test",
"marks": "4"
}
How can I do it from PowerShell?
Your post was not completely clear if you wanted to remove certain keys, or if you only wanted to retain marks and name. The code below performs the latter:
Get-Content 'test.json' -Raw |
ConvertFrom-Json |
Select-Object name, marks |
ConvertTo-Json
Result:
{
"name": "test",
"marks": "4"
}
powershell cmd:
$obj = Get-Content .\aaa.json | ConvertFrom-Json
$obj.psobject.properties.remove('exam')
$obj.psobject.properties.remove('class')
$obj | ConvertTo-Json
output:
{
"name": "test",
"marks": "4"
}

Unable to fetch the json value by using powershell

This is my file structure, I would Like to access Subscription base on the value of "Name"
{
"Topics":
[
{
"Name": "topic1",
"Subscription": "sub1"
},
{
"Name": "topic2",
"Subscription": "sub2"
}
]
}
$json = Get-Content 'path' | Out-String | ConvertFrom-Json
$subscriptions=$json.Topics.Subscription|Where Name -EQ "topic1"
I'm getting the Name by executing following command
$json.Topics.Name
topic1
topic2
$json.Topics.Subscriptions
sub1
sub2
But not sure how to keep this in where clause
Try the following:
$json = #'
{
"Topics":
[
{
"Name": "topic1",
"Subscription": "sub1"
},
{
"Name": "topic2",
"Subscription": "sub2"
}
]
}
'#
$objectFromJson = $json | ConvertFrom-Json
$objectFromJson.Topics | Where-Object Name -eq 'topic1'
The above yields:
Name Subscription
---- ------------
topic1 sub1
If you would like to output only the .Subscription property value, simply use
($objectFromJson.Topics | Where-Object Name -eq 'topic1').Subscription
As for what you tried:
$json.Topics.Subscription
This extracts the values of the Subscription properties, which are mere strings ("sub1" and "sub2"), which don't have a .Name property, so your Where Name -EQ "topic1" filter matches nothing.

jq update contents of one file to another as key value

I am trying to update branches.json.branch2 values from branch2.json.Employes values
Using jq, How can I merge content of one file to another file
Below are the files
I have tried this but it did work, it just prints the original data without updating the details
#!/bin/sh
#call file with branch name for example ./update.sh branch2
set -xe
branchName=$1
fullPath=`pwd`/$1".json"
list=$(cat ${fullPath})
branchDetails=$(echo ${list} | /usr/local/bin/jq -r '.Employes')
newJson=$(cat branches.json |
jq --arg updateKey "$1" --arg updateValue "$branchDetails" 'to_entries |
map(if .key == "$updateKey"
then . + {"value":"$updateValue"}
else .
end) |
from_entries')
echo $newJson &> results.json
branch1.json
{
"Employes": [
{
"Name": "Ikon",
"age": "30"
},
{
"Name": "Lenon",
"age": "35"
}
]
}
branch2.json
{
"Employes": [
{
"Name": "Ken",
"age": "40"
},
{
"Name": "Frank",
"age": "23"
}
]
}
brances.json / results.json fromat
{
"branch1": [
{
"Name": "Ikon",
"age": "30"
},
{
"Name": "Lenon",
"age": "35"
}
],
"branch2": [
{
"Name": "Ken",
"age": "40"
},
{
"Name": "Frank",
"age": "23"
}
]
}
Note: I dont have the list of all the branch files at any given point, so script is responsible only to update the that branch details.
If the file name is the name of the property you want to update, you could utilize input_filename to select the files. No testing needed, just pass in the files you want to update. Just be aware of the order you pass in the input files.
Merge the contents of the file as you see fit. To simply replace, just do a plain assignment.
$ jq 'reduce inputs as $i (.;
.[input_filename|rtrimstr(".json")] = $i.Employes
)' branches.json branch{1,2}.json
Your script would just need to be:
#!/bin/sh
#call file with branch name for example ./update.sh branch2
set -xe
branchName=$1
newJson=$(jq 'reduce inputs as $i (.; .[input_filename|rtrimstr(".json")] = $i.Employees)' branches.json "$branchName.json")

How to save a JSON object to a file using Powershell?

I have converted the following JSON file to powershell representation object.
{
"computer": [
{
"children": [
{
"children": [ {
"children": [ {
"path": "T:\Dropbox\kvaki.html",
"name": "kvaki",
"type": "url",
"url": "http://example.com"
} ],
"path": "T:\Dropbox\",
"name": "Njusha",
"type": "folder"
}, {
"path": "T:\Dropbox\Europa.html",
"name": "Europa",
"type": "url",
"url": "http://example.com"
}, {
"path": "T:\Dropbox\math.html",
"name": "math",
"type": "url",
"url": "http://example.com"
} ],
"path": "T:\Dropbox\",
"name": "Money",
"type": "folder"
}
],
"full_path_on_file_sys": "T:\Dropbox\"
}
]
}
After doing some computations with powershell representation I would like to save it to file as JSON.
But command $jsonRepresentation | ConvertTo-Json | Out-File "D:\dummy_path\file.json" saves it in this way
{
"computer": [
{
"children": " ",
"full_path_on_file_sys": "T:\Dropbox\"
}
]
}
Question: how to achieve correct saving of complex powershell JSON representation?
-depth argument for ConvertTo-Json solves the issue.
$jsonRepresentation | ConvertTo-Json -depth 100 | Out-File "D:\dummy_path\file.json"
Just pipe it to Set-Content, or Out-File:
Get-Process powershell |
ConvertTo-Json |
Set-Content json.txt
If you want to both view the output and save it to file, you can pipe the tee command.
Get-Process powershell | ConvertTo-Json | Tee-Object json.txt
$json.properties.metadata | ConvertTo-Json -Compress
if you are stuck with PowerShell Version 2, the JSON module of Joel Bennett from the 'PowerShell Code Repository' might help.
1) The below command can be used to convert a json to CSV
Example:
Get-Content package.json | Out-String | ConvertFrom-Json | Select parameter1, parameter2, parameter3 | ConvertTo-Csv -NoTypeInformation | Format-Table >> C:\JenkinsWorkspace\Result.csv
Get-Content: This is like "cat" command in linux which will get all data of file "package.json" and converts from Json (Using ConvertFrom-Json function) extracting the details of only required parameters and then converting them into CSV using "ConvertTo-Csv" function without any unwanted Type Headers and formatting them into Table.
2) The above result can also be formatted into proper csv to view it as Excel format without any duplicates and also having Text-To-Column conversion using below command:
Import-Csv "C:\Result.csv" -delimiter "," | Sort-Object _from -Unique | Export-csv "C:\FINAL_REPORT_$date.csv"