JQ merge WooCommerce REST API responce - json

Im trying to parse data from woocommerce from linux console.
Need to take only shipping info and item names with quantity.
im making curl request to wp-json/wc/v2/orders/ORDER_ID
then
jq '{order_id:.id,ship_info:.shipping,items: (.line_items[] | {name , quantity} ) }'
If order contains two items, jq will return two objects
example:
{
"order_id": 1234,
"ship_info": {
"first_name": "Sam",
"last_name": "Fisher",
"company": "",
"address_1": "24 Mega Drive",
"address_2": "",
"city": "Eglinton",
"state": "WA",
"postcode": "6032",
"country": "AU",
"phone": ""
},
"items": {
"name": "Black T-shirt",
"quantity": 1
}
}
{
"order_id": 1234,
"ship_info": {
"first_name": "Sam",
"last_name": "Fisher",
"company": "",
"address_1": "24 Mega Drive",
"address_2": "",
"city": "Eglinton",
"state": "WA",
"postcode": "6032",
"country": "AU",
"phone": ""
},
"items": {
"name": "White T-shirt",
"quantity": 1
}
}
I want merge items and use item's name as a key and item's qty as a value. Please advice how to get output like this
{
"order_id": 1234,
"ship_info": {
"first_name": "Sam",
"last_name": "Fisher",
"company": "",
"address_1": "24 Mega Drive",
"address_2": "",
"city": "Eglinton",
"state": "WA",
"postcode": "6032",
"country": "AU",
"phone": ""
},
"items": {
"White T-shirt": "1",
"Black T-shirt": "1"
}
}

With your current jq query you are iterating over the items inside a generated object. That's why you receive one object per item. Rather than merging them afterwards, don't separate them in the first place.
If you changed your query from
jq '{
order_id: .id,
ship_info: .shipping,
items: (.line_items[] | {name, quantity})
}'
to just
jq '{
order_id: .id,
ship_info: .shipping,
items: .line_items
}'
you'd probably already see that .line_items is actually an array.
To transform it according to your desired output, change that line to one of the followings. They should all yield the same result.
items: (.line_items | map({(.name): .quantity}) | add)
items: (INDEX(.line_items[]; .name) | map_values(.quantity))
items: (reduce .line_items[] as $i ({}; .[$i.name] = .quantity))

Related

How do I convert a nested JSON into a comma separated file for analysis? Below are the details

Need to parse the following Json like the following-
new.json
{
"name": "johnDoe",
"dept": 45,
"details": [
{
"salary": "76566",
"weight": "150",
"height": "160",
"sex": "male",
"country": "Usa",
"State": "NJ"
},
{
"salary": "76560",
"weight": "160",
"height": "180",
"sex": "male",
"country": "Usa",
"State": "NC"
}
]
}
{
"name": "DanLee",
"dept": 46,
"details": [
{
"salary": "76566",
"weight": "180",
"height": "160",
"sex": "male",
"country": "Usa",
"State": "NJ"
},
{
"salary": "76560",
"weight": "190",
"height": "180",
"sex": "male",
"country": "Usa",
"State": "NC"
},
{
"salary": "87888",
"weight": "170",
"height": "160",
"sex": "male",
"country": "Usa",
"State": "NY"
}
]
}
Need name, weight and State information extracted from the above Json format and get a comma separated file for analysis like this:
name,weight,State
johnDoe,150,NJ
johnDoe,160,NC
DanLee,180,NJ
DanLee,190,NC
DanLee,170,NY
I have just started to use Jq in Linux and would like to understand how such conversion can be done with a step by step explanation.
Thanks.
jq -r '.name as $name | .details[] | [$name, .weight, .State] | #csv' foo.json
gives me:
"johnDoe","150","NJ"
"johnDoe","160","NC"
"DanLee","180","NJ"
"DanLee","190","NC"
"DanLee","170","NY"
Breakdown:
.name as $name remember the current name as $name
.details[] expand the details array one by one
[$name, .weight, .State] construct an array for every element of details
#csv use the built-in CSV conversion.
-r: don't wrap output in JSON strings again.

Need to find key-value pair and replace key-value pair in JSON using JQ

I have this JSON
{
"firstName": "Rajesh",
"lastName": "Kumar",
"gender": "man",
"age": 24,
"address": {
"streetAddress": "126 Udhna",
"city": "Surat",
"state": "WB",
"postalCode": "394221"
},
"phoneNumbers": [
{
"type": "home",
"number": "7383627627"
}
]
}
I need to find the value of the "state" key Using JQ and replace the value in JSON. I do not want to fetch it by providing the position of the key, Like
firstName=$(cat sample-json.json | jq -r '.firstName')
My expected output
{
"firstName": "Rajesh",
"lastName": "Kumar",
"gender": "man",
"age": 24,
"address": {
"streetAddress": "126 Udhna",
"city": "Surat",
"state": "Bihar",
"postalCode": "394221"
},
"phoneNumbers": [
{
"type": "home",
"number": "7383627627"
}
]
}
If you're willing to specify .address:
jq '.address.state = "Bihar"' sample-json.json
Otherwise:
jq 'walk(if type == "object" and has("state") then .state = "Bihar" else . end)' sample-json.json
This last will replace all .state values. If you only want to replace the first occurrence:
jq 'first(..|objects|select(has("state"))).state = "Bihar"' sample-json.json
And so on. It would really help all concerned if you could make the requirements clear.

Find a record in json Object if the record has specific key in python

I have a JSON object which has 100000 records. I want a select a record which has specific value to the one of the key
Eg:
[{
"name": "bindu",
"age": "24",
"qualification": "b.tech"
},
{
"name": "naveen",
"age": "23",
"qualification": "b.tech"
},
{
"name": "parvathi",
"age": "23",
"qualification": "m.tech"
},
{
"name": "bindu s",
"status": "married"
},
{
"name": "naveen k",
"status": "unmarried"
}]
now I want to combine the records which are having the name with 'bindu' and 'bindu s. We can achieve this by iterating on the JSON object but since the size is more it is taking more time. Is there any way to make this easy.
I want the output like
[{
"name": "bindu",
"age": "24",
"qualification": "b.tech",
"status": "married"
},
{
"name": "naveen",
"age": "23",
"qualification": "b.tech",
"status": "unmarried"
},
{
"name": "parvathi",
"age": "23",
"qualification": "m.tech"
"status": ""
},
This will rename and merge your objects by first name.
jq 'map(.name |= split(" ")[0]) | group_by(.name) | map(add)'

Python json.loads() returns list instead of dict

I have the following json from a API i'm getting via requests.get().
Now I want to turn this json into a dict with json.loads() but the outcome is a list.
From https://www.w3schools.com/python/python_json.asp the outcome should be a dict... or am I wrong here? Thanks!
import requests
import json
r = requests.get('http://url/api,
auth=requests.auth.HTTPBasicAuth('user','pass'))
x = json.loads(r.text)
# type(x) >>> <class 'list'>
json:
[
{
"id": 400285,
"statusId": 1,
"deliveryAddress": {
"firstName": "Admin",
"lastName": "Admin",
"company": "Testshop 2",
"street": "Teststr. 1",
"houseNumber": "",
"additionalAddressInfo": "",
"postcode": "12345",
"city": "Testort",
"state": "Bremen",
"country": "Germany",
"countryIsoCode": "DE"
},
"billingAddress": {
"firstName": "Admin",
"lastName": "Admin",
}
},
{
"id": 400288,
"statusId": 1,
"deliveryAddress": {
"firstName": "Carolin",
"lastName": "Pr\u00f6ger",
"company": "",
"street": "teststra\u00dfe 12",
"houseNumber": "",
"additionalAddressInfo": "",
"postcode": "12345",
"city": "Bremen",
"state": "Bremen",
"country": "Germany",
"countryIsoCode": "DE"
},
"billingAddress": {
"firstName": "Carolin",
"lastName": "Pr\u00f6ger",
}
}
]
Edit1:
mydict= {}
for item in mylist:
mydict['ID'] = item['id']
mydict['statusID'] = item['statusId']
The data type returned from json.load is assigned depending of the data structure of the json file.
Your json file has [] (list structure).
But you need your json file to have {} (list structure).
It is because of your json response contains list not object.

Combining Nested Json using PowerShell

I have the following Json script:
{
"merchant_info": {
"email": "merchant#example.com",
"first_name": "David",
"last_name": "Larusso",
"business_name": "Mitchell & Murray",
"phone": {
"country_code": "001",
"national_number": "4085551234"
},
"address": {
"line1": "1234 First Street",
"city": "Anytown",
"state": "CA",
"postal_code": "98765",
"country_code": "US"
}
},
"billing_info": [{
"email": "bill-me#example.com",
"first_name": "Stephanie",
"last_name": "Meyers"
}
],
"shipping_info": {
"first_name": "Stephanie",
"last_name": "Meyers",
"address": {
"line1": "1234 Main Street",
"city": "Anytown",
"state": "CA",
"postal_code": "98765",
"country_code": "US"
}
},
"items": [{
"name": "Zoom System wireless headphones",
"quantity": 2,
"unit_price": {
"currency": "USD",
"value": "120"
},
"tax": {
"name": "Tax",
"percent": 8
}
}, {
"name": "Bluetooth speaker",
"quantity": 1,
"unit_price": {
"currency": "USD",
"value": "145"
},
"tax": {
"name": "Tax",
"percent": 8
}
}
],
"discount": {
"percent": 1
},
"shipping_cost": {
"amount": {
"currency": "USD",
"value": "10"
}
},
"note": "Thank you for your business.",
"terms": "No refunds after 30 days."
}
And I want to use PowerShell to get the following Record and export it to CSV:
So far I created the following Script:
$JsonFile = "C:\Users\me\Documents\myfile.json"
$OutputFile = "C:\Users\me\Documents\newtext.csv"
Get-Content -Path $OutputFile
$json = ConvertFrom-Json (Get-Content $JsonFile -Raw)
$json.merchant_info | Select "first_name","last_name",#{Label = "phone"; Expression = {$_.phone.national_number}} |
Export-Csv $OutputFile -NoTypeInformation
I am able to bring values from (Merchant_info, Shipping_info, item) separetely but how do I bring it all in combined like in my screen shot above.
but how do I bring it all in combined like in my screen shot above.
We can only guess; assuming this entire json block is one order, with one merchant and one customer, but multiple items, then each row is an item. So start with that as the input:
Create an output record (PSCustomObject) with the repeated data, and then the individual item data:
$json.items | ForEach-Object {
[PSCustomObject]#{
MerchantInfoFirstName = $json.merchant_info.first_name
MerchantInfoLastName = $json.merchant_info.last_name
MerchantInfoPhoneNumber = $json.merchant_info.phone.national_number
ShippingInfoFirstName = $json.shipping_info.first_name
ShippingInfoLastName = $json.shipping_info.last_name
ItemName = $_.name
ItemQuantity = $_.quantity
}
} | Export-Csv ... etc.