How to get all properties named ref from a json using jmespath? - normalization

I have a json which is as below:
{
"ref": "a",
"details": {
"ref": "b",
"properties": [
{ "ref": "c" },
{ "ref": "d" }
]
}
}
I want to get all the values whose property is "$ref" using JMESpath.
I tried ..ref but it didn't work.
Output should be: ["a", "b", "c", "d"]

You tagged JSON Path as well, so here's that answer. Not sure about JMESPath.
You need to start your path with a $.
$..ref
You can see that it works here.

Related

Given last node of singly linked list, how do we find head node

Given last node of singly linked list, how do we find head node?
Let's say given JSON:
{
"id": "A",
"next": "B"
},
{
"id": "B",
"next": "C"
}
{
"id": "C",
"next": "D"
}
{
"id": "D",
"next": null
}
Now assume that above is not sorted and we need to figure out HEAD element 'A'.
You can use Array.prototype.find to find an element that's id is not the value next of another object in the list. Assuming a valid non-empty singly-linked list there must be exactly one element which satisfies that condition (the head). If the list is empty head will be assigned the value undefined.
const json = '[{"id": "A", "next": "B"}, {"id": "B", "next": "C"},{"id": "C", "next": "D"},{"id": "D", "next": null}]';
const objects = JSON.parse(json);
const head = objects.find( a => ! objects.find( b => a.id === b.next ) );
console.log( head );

JSON - Count number of items in Ruby

I have a Json object as follows:
jsonobject = {
"fruits": [
{
"name": "A",
"uri": "a.com"
},
{
"name": "B",
"uri": "b.com"
}
],
"trees": [
{
"name": "t1",
"uri": "t1.com",
"status": null
},
{
"name": "t2",
"uri": "t2.com",
"status": null
}
]
}
How to get the number of trees/fruits in Ruby?
First of all, a small correction. Your jsonobject should be a string. Enclose the entire jsonobject in single quotes.
jsonobject = '{"fruits": [{"name": "A","uri": "a.com"},{"name": "B","uri": "b.com"}],"trees": [{"name": "t1","uri": "t1.com","status": null},{"name": "t2","uri": "t2.com","status": null}]}'
You can make use of the JSON library of ruby here...
number of trees
JSON.parse(jsonobject)["trees"].size
number of fruits
JSON.parse(jsonobject)["fruits"].size
UPDATE:
How to make the jsonobject as string
jsonobject = %q({"fruits": [{"name": "A","uri": "a.com"},{"name": "B","uri": "b.com"}],"trees": [{"name": "t1","uri": "t1.com","status": null},{"name": "t2","uri": "t2.com","status": null}]})

fill a JSON file with embedded arrays from Rails

I have a simple "rss" (ApplicationRecord) table indexed by an id. I would like to have a structured JSON that group each user from a family in an array structure. And then each family in a global array. How can I do that ?
my current plain code to put my data in a json file is :
json.rss #rss do |rs|
json.id rs.id
json.name rs.name
json.family rs.family
json.lastdate rs.lastdate
json.last rs.last
json.s1w rs.s1w
json.s2w rs.s2w
end
But the target file that I want is this one :
{
"rss": [
{
"familyname": "Smith",
"children": [
{
"id": "1",
"name": "bob",
"lastdate": "2010-09-23",
"last": "0.88",
"s1w": "0.83",
"s2w": "0.88"
},
{
"id": 2,
"name": "Mary",
"lastdate": "2011-09-23",
"last": "0.89",
"s1w": "0.83",
"s2w": "0.87"
}
]
},
{
"familyname": "Wesson",
"children": [
{
"id": "1",
"name": "john",
"lastdate": "2001-09-23",
"last": "0.88",
"s1w": "0.83",
"s2w": "0.88"
},
{
"id": 2,
"name": "Bruce",
"lastdate": "2000-09-23",
"last": "0.89",
"s1w": "0.83",
"s2w": "0.87"
}
]
}
]
}
The grouping you are trying to achieve can be done in Ruby with:
#rss.group_by(&:family).values
This is assuming #rss is an array-like collection of objects that have a .family method. The result: is an array of arrays of objects grouped by family.
Now it will be up to use to use Jbuilder's array! method to build the desired JSON output.

How to return relationship type with Neo4J's Cypher queries?

I am trying to get the relationship type of a very simple Cypher query, like the following
MATCH (n)-[r]-(m) RETURN n, r, m;
Unfortunately this return an empty object for r. This is troublesome since I can't distinguish between the different types of relationships. I can monkey patch this by adding a property like [r:KNOWS {type:'KNOWS'}] but I am wondering if there isn't a direct way to get the relationship type.
I even followed the official Neo4J tutorial (as described below), demonstrating the problem.
Graph Setup:
create (_0 {`age`:55, `happy`:"Yes!", `name`:"A"})
create (_1 {`name`:"B"})
create _0-[:`KNOWS`]->_1
create _0-[:`BLOCKS`]->_1
Query:
MATCH p=(a { name: "A" })-[r]->(b)
RETURN *
JSON RESPONSE BODY:
{
"results": [
{
"columns": [
"a",
"b",
"p",
"r"
],
"data": [
{
"row": [
{
"name": "A",
"age": 55,
"happy": "Yes!"
},
{
"name": "B"
},
[
{
"name": "A",
"age": 55,
"happy": "Yes!"
},
{},
{
"name": "B"
}
],
{}
]
},
{
"row": [
{
"name": "A",
"age": 55,
"happy": "Yes!"
},
{
"name": "B"
},
[
{
"name": "A",
"age": 55,
"happy": "Yes!"
},
{},
{
"name": "B"
}
],
{}
]
}
]
}
],
"errors": []
}
As you can see, I get an empty object for r, which makes it impossible to distinguish between the relationships.
NOTE: I am running Neo4J v.2.2.2
Use the type() function.
MATCH (n)-[r]-(m) RETURN type(r);
Added distinct.
MATCH (n)-[r]-(m) RETURN distinct type(r);

Mongo query in Object of arrays

Document example
{
"_id": 1,
"test": {
"item_obj": {
"item1": ["a", "b"],
"item2": ["c"],
"item3": ["a", "d"]
}
}
}
I want to fetch documents where "a" exists in test.item_obj. "a" may exist in any array. And we don't know the keys present inside item_obj (No idea item1, item2 or item3 exists).
Need rails-mongo query.
If this is your search case, then whatever way you look at it you need the JavaScript evaluation of the $where clause to resolve your current structure. In the shell example ( since you need to use the JavaScript expression anyway ):
db.collection.find(function() {
var root = this.test.item_obj;
return Object.keys(root).some(function(key) {
return root[key] == "a";
});
})
Or for mongoid that is something like:
func = <<-eof
var root = this.test.item_obj;
return Object.keys(root).some(function(key) {
return root[key] == "a";
});
eof
Model.for_js(func)
However, if you simply change your structure to define "items_objects" as an array as follows:
{
"_id": 1,
"test": {
"item_objects": [
{ "name": "item1", "data": ["a","b"] },
{ "name": "item2", "data": ["c"] },
{ "name": "item3", "data": ["a","d"] }
}
}
}
Then asking for what you want here is as basic as:
db.collection.find({
"test.item_objects.data": "a"
})
Or for mongoid:
Model.where( "test.item_objects.data" => "a" )
Nested arrays are not really a great idea though, so perhaps live with:
{
"_id": 1,
"test": {
"item_objects": [
{ "name": "item1", "data": "a" },
{ "name": "item1", "data": "b" },
{ "name": "item2", "data": "c" },
{ "name": "item3", "data": "a" },
{ "name": "item3", "data": "d" }
}
}
}
Which is basically the same thing, but a but more long winded. But ultimately much more easy to deal with in atomic updates. And of course the query to find the values in the document is exactly the same.

Resources