Remove json object from array of objects in mysql - mysql

I have a column userinventory in mysql table with below data and I want to remove one of json object where "sellFlag":"y" and "product_id":"1"
[{
"price": "250",
"category": "Furniture",
"sellFlag": "y",
"assetLink": "Furniture/Table",
"product_id": "1"
},
{
"price": "175",
"category": "Furniture",
"assetLink": "Furniture/IkeaStockholmCoffeeTableBlack",
"product_id": "31"
},
{
"price": "300",
"category": "Furniture",
"assetLink": "Furniture/ZanottaDamaSofaMultiseater",
"product_id": "29"
},
{
"price": "200",
"category": "Furniture",
"assetLink": "Furniture/RoundTable",
"product_id": "9"
}]
I have tried select JSON_SEARCH(userInventory,'all','y', NULL,'$[*].sellFlag') as uInvent FROM user_information WHERE user_id=50 its giving result "$[27].sellFlag" but I need to add condition for product_id too
After successful removal, below result is required.
[{
"price": "175",
"category": "Furniture",
"assetLink": "Furniture/IkeaStockholmCoffeeTableBlack",
"product_id": "31"
},
{
"price": "300",
"category": "Furniture",
"assetLink": "Furniture/ZanottaDamaSofaMultiseater",
"product_id": "29"
},
{
"price": "200",
"category": "Furniture",
"assetLink": "Furniture/RoundTable",
"product_id": "9"
}]

You can convert it to collection of objects and then loop them in foreach
and test any conditions and remove any object
and in the end convert it again to json

Related

Access a key outside of an array and store in a variable

I have this bit of json:
[{
"id": 123,
"name": "Ice",
"categories": [{
"products": [{
"id": "66642",
"display_name": "Ice A"
},
{
"id": "62466",
"display_name": "Ice B"
},
{
"id": "62466",
"display_name": "Ice B duplicate"
}
]
}]
},
{
"id": 97,
"name": "Cakes",
"categories": [{
"products": [{
"id": "14518",
"display_name": "Cake 1"
},
{
"id": "14105",
"display_name": "Cake 2"
}
]
}]
}
]
I'm using jq 1.6 and I'm trying to get this output:
[{
"id": "62466",
"name": "Ice B",
"category": "Ice"
},
{
"id": "66642",
"name": "Ice A",
"category": "Ice"
},
{
"id": "14105",
"name": "Cake 2",
"category": "Cakes"
},
{
"id": "14518",
"name": "Cake 1",
"category": "Cakes"
}]
I've tried with this code:
.[].name as $x | map(.categories[].products[]) | unique_by(.id) | .[] |
[{
id: .id,
name: .display_name,
category: $x
}]
But I get this output:
[
{
"id": "14105",
"name": "Cake 2",
"category": "Ice"
}
]
[
{
"id": "14518",
"name": "Cake 1",
"category": "Ice"
}
]
[
{
"id": "62466",
"name": "Ice B",
"category": "Ice"
}
]
[
{
"id": "66642",
"name": "Ice A",
"category": "Ice"
}
]
[
{
"id": "14105",
"name": "Cake 2",
"category": "Cakes"
}
]
[
{
"id": "14518",
"name": "Cake 1",
"category": "Cakes"
}
]
[
{
"id": "62466",
"name": "Ice B",
"category": "Cakes"
}
]
[
{
"id": "66642",
"name": "Ice A",
"category": "Cakes"
}
]
I looks like as there are two names
Ice
Cakes
It's multiplying the output by two , one for each name instead of just returning the 4 products and I can't get the result into and array of objects. Here is a reproducible code in jqplay. Because there are some duplicate records, ie: products that have more than one category I need them to be unique. How can i fix this?
Assigning the category name in a variable and then transforming each product with unique id:
map(
.name as $category
| .categories[].products
| unique_by(.id)
| .[]
| { id, name: .display_name, $category }
)
This map produces the 4 unique items.
jq 'map(
(.categories[].products | unique_by(.id)[] | {id, name: .display_name})
+ {category: .name}
)'
[
{
"id": "62466",
"name": "Ice B",
"category": "Ice"
},
{
"id": "66642",
"name": "Ice A",
"category": "Ice"
},
{
"id": "14105",
"name": "Cake 2",
"category": "Cakes"
},
{
"id": "14518",
"name": "Cake 1",
"category": "Cakes"
}
]
Demo

How to sum values in json using jq

I just started using jq parser as i need to parse some values out of a json file and make few changes and output an updated json file with the changes. Here is one of the samples out of many i'm working on.
input json:
{
"name": ".",
"metadata": {
"path": ".",
"type": "terraform_dir",
"terraformWorkspace": "default"
},
"pastBreakdown": {
"resources": [],
"totalHourlyCost": "0",
"totalMonthlyCost": "0"
},
"breakdown": {
"resources": [
{
"name": "vm01",
"metadata": {},
"hourlyCost": "0.007989726027400584",
"monthlyCost": 3.9660999999999986,
"costComponents": [
{
"name": "vm01-201",
"unit": "years",
"hourlyQuantity": "0.0001141552511416",
"monthlyQuantity": "0.0833333333333333",
"price": "69.99",
"hourlyCost": "0.007989726027400584",
"monthlyCost": "5.832499999999997667"
}
]
},
{
"name": "public_ip.example",
"metadata": {},
"hourlyCost": "0.005",
"monthlyCost": 2.482,
"costComponents": [
{
"name": "IP address (static)",
"unit": "hours",
"hourlyQuantity": "1",
"monthlyQuantity": "730",
"price": "0.005",
"hourlyCost": "0.005",
"monthlyCost": "3.65"
}
]
},
{
"name": "storage_account",
"metadata": {},
"hourlyCost": "0.17575342465753424425",
"monthlyCost": 87.24400000000001,
"costComponents": [
{
"name": "Write",
"unit": "10K operations",
"hourlyQuantity": "0.136986301369863",
"monthlyQuantity": "100",
"price": "0.1",
"hourlyCost": "0.0136986301369863",
"monthlyCost": "10"
},
{
"name": "List",
"unit": "10K operations",
"hourlyQuantity": "0.136986301369863",
"monthlyQuantity": "100",
"price": "0.05",
"hourlyCost": "0.00684931506849315",
"monthlyCost": "5"
},
{
"name": "Read",
"unit": "10K operations",
"hourlyQuantity": "0.0136986301369863",
"monthlyQuantity": "10",
"price": "0.01",
"hourlyCost": "0.000136986301369863",
"monthlyCost": "0.1"
},
{
"name": "All other operations",
"unit": "10K operations",
"hourlyQuantity": "0.136986301369863",
"monthlyQuantity": "100",
"price": "0.004",
"hourlyCost": "0.000547945205479452",
"monthlyCost": "0.4"
},
{
"name": "retrieval",
"unit": "GB",
"hourlyQuantity": "1.3698630136986301",
"monthlyQuantity": "1000",
"price": "0.01",
"hourlyCost": "0.013698630136986301",
"monthlyCost": "10"
},
{
"name": "write",
"unit": "GB",
"hourlyQuantity": "1.3698630136986301",
"monthlyQuantity": "1000",
"price": "0.0025",
"hourlyCost": "0.00342465753424657525",
"monthlyCost": "2.5"
},
{
"name": "index",
"unit": "10K tags",
"hourlyQuantity": "0.0136986301369863",
"monthlyQuantity": "10",
"price": "0.03",
"hourlyCost": "0.000410958904109589",
"monthlyCost": "0.3"
}
]
}
],
"totalMonthlyCost": "sum value"
From the above json i need to sum the monthlyCost of each resources and update total value in totalMonthlyCost
So the input would be "3.9660999999999986 + 2.482 + 87.24400000000001" the sum of these values should be updated in totalMonthlyCost
I have been trying various options but no luck. I could calculate the values within the array
but not sure how to update the value in totalMonthlyCost
In two steps in accordance with the task description:
(.breakdown.resources | map(.monthlyCost) | add) as $sum
| .breakdown.totalMonthlyCost = $sum
This can be written without the intermediate variable:
.breakdown.totalMonthlyCost
= (.breakdown.resources | map(.monthlyCost) | add)
or more DRYly:
.breakdown
|= (.totalMonthlyCost = (.resources | map(.monthlyCost) | add))

Nested json - store values in csv

I am trying to convert a nested json file into csv. It's data from a darts API and the structure is always the same. Nevertheless I got some problems flattening and storing the values in a csv because of the nested structure.
json:
{
"summaries": [{
"sport_event": {
"id": "sr:sport_event:12967512",
"start_time": "2017-11-11T13:15:00+00:00",
"start_time_confirmed": true,
"sport_event_context": {
"sport": {
"id": "sr:sport:22",
"name": "Darts"
},
"category": {
"id": "sr:category:104",
"name": "International"
},
"competition": {
"id": "sr:competition:597",
"name": "Grand Slam of Darts"
},
"season": {
"id": "sr:season:47332",
"name": "Grand Slam of Darts 2017",
"start_date": "2017-11-11",
"end_date": "2017-11-20",
"year": "2017",
"competition_id": "sr:competition:597"
},
"stage": {
"order": 1,
"type": "league",
"phase": "stage_1",
"start_date": "2017-11-11",
"end_date": "2017-11-15",
"year": "2017"
},
"round": {
"number": 1
},
"groups": [{
"id": "sr:league:29766",
"name": "Grand Slam of Darts 2017, Group G",
"group_name": "G"
}]
},
"coverage": {
"live": true
},
"competitors": [{
"id": "sr:competitor:35936",
"name": "Smith, Michael",
"abbreviation": "SMI",
"qualifier": "home"
}, {
"id": "sr:competitor:83895",
"name": "Wilson, James",
"abbreviation": "WIL",
"qualifier": "away"
}]
},
"sport_event_status": {
"status": "closed",
"match_status": "ended",
"home_score": 5,
"away_score": 3,
"winner_id": "sr:competitor:35936"
}
}, {
"sport_event": {
"id": "sr:sport_event:12967508",
"start_time": "2017-11-11T13:40:00+00:00",
"start_time_confirmed": true,
"sport_event_context": {
"sport": {
"id": "sr:sport:22",
"name": "Darts"
},
"category": {
"id": "sr:category:104",
"name": "International"
},
"competition": {
"id": "sr:competition:597",
"name": "Grand Slam of Darts"
},
"season": {
"id": "sr:season:47332",
"name": "Grand Slam of Darts 2017",
"start_date": "2017-11-11",
"end_date": "2017-11-20",
"year": "2017",
"competition_id": "sr:competition:597"
},
"stage": {
"order": 1,
"type": "league",
"phase": "stage_1",
"start_date": "2017-11-11",
"end_date": "2017-11-15",
"year": "2017"
},
"round": {
"number": 1
},
"groups": [{
"id": "sr:league:29764",
"name": "Grand Slam of Darts 2017, Group F",
"group_name": "F"
}]
},
"coverage": {
"live": true
},
"competitors": [{
"id": "sr:competitor:70916",
"name": "Bunting, Stephen",
"abbreviation": "BUN",
"qualifier": "home"
}, {
"id": "sr:competitor:191262",
"name": "de Zwaan, Jeffrey",
"abbreviation": "DEZ",
"qualifier": "away"
}]
},
"sport_event_status": {
"status": "closed",
"match_status": "ended",
"home_score": 5,
"away_score": 4,
"winner_id": "sr:competitor:70916"
}
}
So for each sport_event I would like to store the variables:
"start_time"
from "season" the variable "name"
from "competitors" both "id" and "name"
from "sport_event_status" the "winner_id"
I have already tried to flatten the json file with this code:
import json
f = open(r'path of file.json')
data = json.load(f)
def flatten(data):
for key,value in data.items():
print (str(key)+'->'+str(value))
if type(value) == type(dict()):
flatten(value)
elif type(value) == type(list()):
for val in value:
if type(val) == type(str()):
pass
elif type(val) == type(list()):
pass
else:
flatten(val)
flatten(data)
print(data)
This actually prints out the following:
id->sr:season:47332
name->Grand Slam of Darts 2017
start_date->2017-11-11
end_date->2017-11-20
year->2017
competition_id->sr:competition:597
Now my question is how to store the values I mentioned above in a csv file.
Thanks in advance for your support.
Using jq, you basically just have to transcribe your specification, adding a bit of context and taking care of an embedded array:
.summaries[]
| .sport_event # Your specification:
| [.start_time, # start_time
.sport_event_context.season.name] # from "season" the variable "name"
+ [.competitors[] | .id, .name] # from "competitors" both "id" and "name"
+ [.sport_event_status.winner_id] # from "sport_event_status" the "winner_id"
| #csv
Invocation
E.g.
jq -rf program.jq my.json

Convert nested json to csv to sheets json api

I'm want to make my json to csv so that i can upload it on google sheets and make it as json api. Whenever i have change data i will just change it on google sheets. But I'm having problems on converting my json file to csv because it changes the variables whenever i convert it. I'm using https://toolslick.com/csv-to-json-converter to convert my json file to csv.
What is the best way to convert json nested to csv ?
JSON
{
"options": [
{
"id": "1",
"value": "Jumbo",
"shortcut": "J",
"textColor": "#FFFFFF",
"backgroundColor": "#00000"
},
{
"id": "2",
"value": "Hot",
"shortcut": "D",
"textColor": "#FFFFFF",
"backgroundColor": "#FFFFFF"
}
],
"categories": [
{
"id": "1",
"order": 1,
"name": "First Category",
"active": true
},
{
"id": "2",
"order": 2,
"name": "Second Category",
"shortcut": "MT",
"active": true
}
],
"products": [
{
"id": "03c6787c-fc2a-4aa8-93a3-5e0f0f98cfb2",
"categoryId": "1",
"name": "First Product",
"shortcut": "First",
"options": [
{
"optionId": "1",
"price": 23
},
{
"optionId": "2",
"price": 45
}
],
"active": true
},
{
"id": "e8669cea-4c9c-431c-84ba-0b014f0f9bc2",
"categoryId": "2",
"name": "Second Product",
"shortcut": "Second",
"options": [
{
"optionId": "1",
"price": 11
},
{
"optionId": "2",
"price": 20
}
],
"active": true
}
],
"discounts": [
{
"id": "1",
"name": "S",
"type": 1,
"amount": 20,
"active": true
},
{
"id": "2",
"name": "P",
"type": 1,
"amount": 20,
"active": true
},
{
"id": "3",
"name": "G",
"type": 2,
"amount": 5,
"active": true
}
]
}
Using python, this can be easily done or almost done. Maybe this code will help you in some way to understand that.
import json,csv
data = []
with open('your_json_file_here.json') as file:
for line in file:
data.append(json.loads(line))
length = len(data)
with open('create_new_file.csv','w') as f:
writer = csv.writer(f)
writers = csv.DictWriter(f, fieldnames=['header1','header2'])
writers.writeheader()
for iter in range(length):
writer.writerow((data[iter]['specific_col_name1'],data[iter]['specific_col_name2']))
f.close()

jquery object as parameter to function

This is my jquery response:
[
{ "depot":
{
"id": "D1",
"intersection": {
"first": "Markham",
"second": "Lawrence"
},
"address": {
"number": "25",
"street": "Cougar Court",
"city": "Scarborough",
"province": "ON",
"postal_code": "M1J"
}
},
"vehicle": [
{
"id": "V1",
"depot_id": "D1",
"model": "Ford Focus",
"price": "45",
"km_per_litre": "15",
"cargo_cu_m": "YES",
"category": "Compact car",
"image": "www.coolcarz.com"
}
,
{
"id": "V2",
"depot_id": "D1",
"model": "Honda Civic",
"price": "45",
"km_per_litre": "150",
"cargo_cu_m": "YES",
"category": "Compact car",
"image": "www.coolcarz.com"
}
,
{
"id": "V8",
"depot_id": "D1",
"model": "Pontiac Aztek",
"price": "10",
"km_per_litre": "6",
"cargo_cu_m": "YES",
"category": "SUV",
"image": "www.nocoolcarz.com"
}
,
{
"id": "V12",
"depot_id": "D1",
"model": "Chevrolet Impala",
"price": "45",
"km_per_litre": "12",
"cargo_cu_m": "YES",
"category": "Standard car",
"image": "www.coolcarz.com"
}
,
{
"id": "V29",
"depot_id": "D1",
"model": "Nissan Leaf",
"price": "150",
"km_per_litre": "0",
"cargo_cu_m": "YES",
"category": "Electronic Car",
"image": "www.coolcarz.com"
}
]
}
,
{ "depot":
{
"id": "A1",
"intersection": {
"first": "Markham",
"second": "Lawrence"
},
"address": {
"number": "25",
"street": "Cougar Court",
"city": "Scarborough",
"province": "ON",
"postal_code": "m1J"
}
},
"vehicle": [
]
}
]
What I want to do is that at some point in my code, when I have received this response data, I want to pass , say data[0] or or data[0].vehicle[1] to a function
The way I am doing it now is:
function(data) {
var items = [];
for( i=0; i<data.length; i++){
items.push('<b>' + data[i].depot.intersection.first+"-"+ data[i].depot.intersection.second + " depot has following cars:"+ '</b>');
for( k=0; k<data[i].vehicle.length;k++){
str = '<li> ' + data[i].vehicle[k].category +", $"+ data[i].vehicle[k].price +' a day </li>';
items.push(str);
in effect I am trying to create a hyperlink (with vehicle category as text), and when user clicks on this hyperlink, I want to pass on the vehicle information array to a new function called moreInfo that does its job. Rit now when I do this and click the hyperlink, I see the error
missing ] after element list
timepass([object Object],[object Object]
Any ideas?
Remove 2 square brackets from beginning and end of your json code and use use it as an object not as an array.
What you did was creating an array of objects not JSON object