I have have Json data stored in Json type field in MySql 5.7.
Here is my Json data structure:
{
"111" :[
{
"id" : 1,
"name" : "John",
"age" : 31,
"group": 111
},
{
"id" : 2,
"name" : "Paul",
"age" : 30,
"group": 111
},
],
"222" :[
{
"id" : 3,
"name" : "Jack",
"age" : 31,
"group": 222
},
{
"id" : 4,
"name" : "Robin",
"age" : 30,
"group": 222
},
]
}
and i want to extract the data as a table-like structure with MySql statement. The idea is to get like this :
i already try this
SELECT JSON_EXTRACT(jsonData -> '$."111"', '$[0]."name"') as name from table_t
but it just extract per key and index.
Please help,
Thank you
Related
I have a JSON file which contains unneeded values. I want to filter them by only selecting those I need, or deleting those I don't need. For example, I have this JSON file:
{
{ "id" : "1",
"key1" : "val1",
"key2" : "val2",
"name" : "someone",
"age" : 39,
},
{ "id" : "1234",
"key1" : "val1",
"key2" : "val2",
"name" : "someone",
"age" : 39
},
{ "id" : "4567",
"key1" : "val1",
"key2" : "val2",
"name" : "someone",
"age" : 60
}
}
My aim is to get the following JSON, by deleting all key1 and key2 items recursively using a jq command:
{
{ "id" : "1",
"name" : "someone",
"age" : 39,
},
{ "id" : "1234",
"name" : "someone",
"age" : 39
},
{ "id" : "4567",
"name" : "someone",
"age" : 60
}
}
Thank you.
If the input is supposed to be an array of objects (then format it as [{…},{…},{…}]), you can use map to apply filters or a new formatting onto each element of the array.
Construct what you need (Demo):
jq 'map({id,name,age})'
Or remove what you don't need (Demo):
jq 'map(del(.key1,.key2))'
Both produce:
[
{
"id": "1",
"name": "someone",
"age": 39
},
{
"id": "1234",
"name": "someone",
"age": 39
},
{
"id": "4567",
"name": "someone",
"age": 60
}
]
I am attempting get a value based on the key containing # symbol fails using JSONPath.
Cause: Since # is the current object/element, json does not return the value.
Sample 1:
{
"firstName": "John",
"lastName" : "doe",
"phoneNumbers": [
{
"type" : "iPhone",
"number": "0123-4567-8888"
},
{
"type" : "home",
"number": "0123-4567-8910"
}
]
}
This works: $.phoneNumbers[1].type
This works: $.phoneNumbers[?(#.type=="iPhone")].type
Sample2:
{
"firstName": "John",
"lastName" : "doe",
"phoneNumbers": [
{
"#type" : "iPhone",
"number": "0123-4567-8888"
},
{
"#type" : "home",
"number": "0123-4567-8910"
}
]
}
This works: $.phoneNumbers[1].type
This does not work: $.phoneNumbers[?(#.#type=="iPhone")].type
Any advice for dealing with keys and values containing # characters?
It depends which jsonpath implementation you are using.
If you are using JsonPath-Plus then you can try something like:
$.phoneNumbers[?(#['#type'] == "iPhone")]
You can evaluate jsonpath online here which is based on JsonPath-Plus.
I am new to mongodb , I have two collections like this :
1st collection name is A
{
"_id": "1234",
"versions": [{
"owner_id": ObjectId("100000"),
"versions": 1,
"type" : "info",
"items" : ["item1","item3","item7"]
},
{
"owner_id": ObjectId("100001"),
"versions": 2,
"type" : "bug",
"OS": "Ubuntu",
"Dependencies" : "Trim",
"items" : ["item1","item7"]
}
]}
2nd Collection name is B
{ "_id": ObjectId("100000"), "email": "abc#xyz.com" } { "_id": ObjectId("100001"), "email": "bbc#xyz.com"}
Expected output is :
{
"_id": "1234",
"versions":[{
"owner_id": "abc#xyz.com",
"versions": 1,
"type" : "info",
"items" : ["item1","item3","item7"]
},
{
"owner_id": "bbc#xyz.com",
"versions": 2,
"type" : "bug",
"OS": "Ubuntu",
"Dependencies" : "Trim",
"items" : ["item1","item7"]
}
] }
I used mongo $lookup but I am not getting required output
Please help.
Thank You!!!
You need to $unwind versions, $lookup with another collection on foreignField, $project to take the first element from the match array, $group to get back in original document format
collection a
> db.a.find()
{ "_id" : "1234", "versions" : [ { "owner_id" : "100000" }, { "owner_id" : "100001" }, { "owner_id" : "100001" } ] }
collection b
> db.b.find()
{ "_id" : "100000", "email" : "abc#xyz.com" }
{ "_id" : "100001", "email" : "bbc#xyz.com" }
aggregate pipeline
> db.a.aggregate(
[
{$unwind:"$versions"},
{$lookup : {from : "b", "localField":"versions.owner_id", "foreignField":"_id", as :"out"}},
{$project : {"_id":1, "versions.owner_id":{$arrayElemAt:["$out.email",0]}}},
{$group:{_id:"$_id", versions : {$push : "$versions"}}}
]
).pretty()
output
{
"_id" : "1234",
"versions" : [
{
"owner_id" : "abc#xyz.com"
},
{
"owner_id" : "bbc#xyz.com"
},
{
"owner_id" : "bbc#xyz.com"
}
]
}
{
"employees" : [
{
"name" : "XXX",
"id" : "1",
"Salary" : [
{
"Month" : "XXXX",
"Amount" : "XXXX",
},
{
"Month" : "XXXX",
"Amount" : "XXXX",
},
{
"Month" : "XXXX",
"Amount" : "XXXX",
}
]
},
{
"name" : "YYY",
"id" : "2",
"Salary" : [
{
"Month" : "YYYY",
"Amount" : "YYYY",
},
{
"Month" : "YYYY",
"Amount" : "YYYY",
},
{
"Month" : "YYYY",
"Amount" : "YYYY",
}
]
}
],
}
This is sample of json format in mongodb document.
I want to get the result as a one particular element from employees Array that search by name
I tried these methods
db.abc.find({"employees.name": "XXX"},{employees: {$elemMatch: {name: "XXX"}}});
and
db.abc.find({ employees: { $elemMatch: { name: "XXX"} } })
none of those won't work. those methods give whole document as a result. can anyone give me a solution on that.
Try this
db.abc.find({"employees.name": "XXX"},{"employees.$":1})
I am having trouble with the syntax (SyntaxError: Unexpected token ILLEGAL) in MongoDB. This command was copied directly from a MongoDB instruction PDF and I cannot find out what is wrong.
Also I don't know if it is relevant but I am using Codeanywhere with a MEAN stack.
db.restaurants.insert(
{
"address" : {
"street" : "2 Avenue",
"zipcode" : "10075",
"building" : "1480",
"coord" : [ 73.9557413, 40.7720266 ],
},
"borough" : "Manhattan",
"cuisine" : "Italian",
"grades" : [
{
"date" : ISODate("20141001T00:00:00Z"),
"grade" : "A",
"score" : 11
},
{
"date" : ISODate("20140116T00:00:00Z"),
"grade" : "B",
"score" : 17
}
],
"name" : "Vella",
"restaurant_id" : "41704620"
}
)
Try to replace:
"coord" : [ 73.9557413, 40.7720266 ],
with:
"coord" : [ 73.9557413, 40.7720266 ]
The comma at the end of subdocument is extra.
By the way, the JSON standard allows only double quoted string as property key, thus, try also this variant:
"coord" : [ "73.9557413", "40.7720266" ]
I checked your entire JSON-document with a JSON validator, here is a valid version:
{
"address": {
"street": "2 Avenue",
"zipcode": "10075",
"building": "1480",
"coord": ["73.9557413", "40.7720266"]
},
"borough": "Manhattan",
"cuisine": "Italian",
"grades": [{
"date": "20141001T00:00:00Z",
"grade": "A",
"score": 11
}, {
"date": "20140116T00:00:00Z",
"grade": "B",
"score": 17
}],
"name": "Vella",
"restaurant_id": "41704620"
}