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
}
]
Related
I am trying to convert one json format into another as follows:-
I already have a Response POJO :-
class Response {
String id;
List<ResponseRowValues> values;
}
class ResponseRowValues {
String path;
List<String> values;
}
which is giving the json in this format :-
{
"id" : "12345",
"values" : [
{
"path": "Path A",
"values" : [ "val1", "val2"
]
},
{
"path": "Path A/Path B",
"values" : [ "val3", "val4"
]
},
{
"path": "Path A/Path B/Path C",
"values" : [ "val5", "val6"
]
}
]
}
I am trying to convert above json format into :-
{
"id" : "12345",
"data" : {
"record": [
{
"name" : "Path A",
"children" : [
{
"name": "Path B",
"children" : [
{
"name": "Path C",
"children" : [],
"values" : ["val5", "val6" ]
}
],
"values" : [
"val3", "val4"
]
}
],
"values" : [
"val1", "val2"
]
}
]
}
}
Trying to create a tree structure in java so that I can create json in above format.
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
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 trying to figure out how oneOf works by building a schema which validates two different object types. For example a person (firstname, lastname, sport) and vehicles (type, cost).
Here are some sample objects:
{"firstName":"John", "lastName":"Doe", "sport": "football"}
{"vehicle":"car", "price":20000}
The question is what have I done wrongly and how can I fix it. Here is the schema:
{
"description": "schema validating people and vehicles",
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"required": [ "oneOf" ],
"properties": { "oneOf": [
{
"firstName": {"type": "string"},
"lastName": {"type": "string"},
"sport": {"type": "string"}
},
{
"vehicle": {"type": "string"},
"price":{"type": "integer"}
}
]
}
}
When I try to validate it in this parser:
https://json-schema-validator.herokuapp.com/
I get the following error:
[ {
"level" : "fatal",
"message" : "invalid JSON Schema, cannot continue\nSyntax errors:\n[ {\n \"level\" : \"error\",\n \"schema\" : {\n \"loadingURI\" : \"#\",\n \"pointer\" : \"/properties/oneOf\"\n },\n \"domain\" : \"syntax\",\n \"message\" : \"JSON value is of type array, not a JSON Schema (expected an object)\",\n \"found\" : \"array\"\n} ]",
"info" : "other messages follow (if any)"
}, {
"level" : "error",
"schema" : {
"loadingURI" : "#",
"pointer" : "/properties/oneOf"
},
"domain" : "syntax",
"message" : "JSON value is of type array, not a JSON Schema (expected an object)",
"found" : "array"
} ]
Try this:
{
"description" : "schema validating people and vehicles",
"type" : "object",
"oneOf" : [
{
"type" : "object",
"properties" : {
"firstName" : {
"type" : "string"
},
"lastName" : {
"type" : "string"
},
"sport" : {
"type" : "string"
}
}
},
{
"type" : "object",
"properties" : {
"vehicle" : {
"type" : "string"
},
"price" : {
"type" : "integer"
}
},
"additionalProperties":false
}
]
}
oneOf need to be used inside a schema to work.
Inside properties, it's like another property called "oneOf" without the effect you want.