How to list object key names with jsonpath? - json

I am using nodejs with jsonpath.
I have this json structure:
{
things:{
books: [
{name: "book1"},
{name: "book2"},
{name: "book3"},
{name: "book4"},
],
movies: [
{name: "movie1"},
{name: "movie2"},
{name: "movie3"},
{name: "movie4"},
]
}
}
I would like to know the jsonpath expression that returns an array with the key names of the things object. That would be:
["books","movies"]
For now, I am doing this:
Object.keys(jsonpath.eval(jsonStructure,"$.things").pop());
But I don't find it elegant... I should not need to get a copy the whole structure when I only need the key names.

jsonPath has new update jsonpath-plus
jsonpath-plus expands on the original specification to add some additional operators and makes explicit some behaviors the original did not spell out.
^ for grabbing the parent of a matching item
~ for grabbing property names of matching items (as array)
so to get proper output use this query things.*~
you can try here also https://jsonpath.com/

I don't believe there is a better solution than your own:
Object.keys(jsonpath.eval(jsonStructure,"$.things").pop());
I think the main misconception here is that you don't have to worry about this snippet "getting a copy of the whole structure", because you aren't copying the whole structure. You already have the entire object loaded into memory, jsonpath doesn't create a new copy, it simply returns a reference to the already existing object, i.e.:
jsonpath.eval(jsonStructure,"$.things").pop() === jsonStructure.things //true

Not exactly what you are asking for, but might still be relevant.
We use object-scan for this kind of task as it is much better suited for data processing and analyzing. Once you wrap your head around it that is (:
Anyways, here is how you could answer your question if you are willing to add another dependency
// const objectScan = require('object-scan');
const data = { things: { books: [ { name: 'book1' }, { name: 'book2' }, { name: 'book3' }, { name: 'book4' } ], movies: [ { name: 'movie1' }, { name: 'movie2' }, { name: 'movie3' }, { name: 'movie4' } ] } };
console.log(objectScan(['things.*'], { rtn: 'property' })(data));
// => [ 'movies', 'books' ]
.as-console-wrapper {max-height: 100% !important; top: 0}
<script src="https://bundle.run/object-scan#13.7.1"></script>
Disclaimer: I'm the author of object-scan

The syntax you used for give is wrong
to get keys in json path use "$.*~"
ex.
input:
{
"firstName": "John",
"lastName" : "doe",
"age" : 26
}
output:
[
"firstName",
"lastName",
"age"
]

Related

Having a problem with iterating over multiple json objects and showing them in div with vue.js

Trying to create a website that takes in information from an API, however I don't really understand how to do it seeing that I need all results grouped up and the API I've created almost never gives a response with the same amounts of objects. So the question is, seeing that I use vue.js and axios is there any way to loop through the json objects to show each of the objects in a seperate ? I manage to do it when there are a specified amounts, but I want to make it dynamic so I don't hardcode into the variables what part of the response I need to set to each variable.
UPDATE: I've tried to use v-for, but seeing that I need to have the output quite structured it doesn't really help, I've also tried Nested V-for loops, once again I can't get the accuracy that I'm looking for.
UPDATE2: Also should be added, when I say JSON object I actually ment js object. the json.parse() has been used on the json.
UPDATE3: Updated the JSON to actual data that I'm using for the application.
Every div need a lemma, a paradigm tagset, inflection tagset and inflectionForms and a table for all the meanings. Just need meaning not meaningText. TranslationId is not important. The JTranslate that wraps every object will be removed, just kinda tired of the Java at the moment, will do that later today and do the adjustments on the vue projects aswell regarding that deletion.
Actually your json format is invalid
{
"object1":{
"name": "test",
"data": "test"
},
"object2":{
"name": "test2",
"data": "test2"
},
"object3":{
"name": "test2",
"data": "test2"
}
}
it should be like above and use JSON.parse() method to simply convert the json to javascript object
Valid Object:
var objects = {
"object1":{
"name": "test",
"data": "test"
},
"object2":{
"name": "test2",
"data": "test2"
},
"object3":{
"name": "test2",
"data": "test2"
}
}
for iteration use
<div v-for="(object,index) in objects" :key="index">
{{object}}
</div>
The correct object as an array:
test: [
{
object1: {
name: 'name1',
data: 'content1'
}
},
{
object2: {
name: 'name1',
data: 'content1'
}
},
{
object3: {
name: 'name3',
data: 'content3'
}
}
]
can be mapped as a computed property inside the script tages:
computed: {
mappedTest() {
return this.test.map(entry => {
const key = Object.keys(entry)[0];
return { name: entry[key].name, data: entry[key].data };
});
}
},
and call it inside the template
<div
v-for="testObject in mappedTest"
:key="testObject"
>
name: {{testObject.name}}; data: {{testObject.data}}
</div>
I was very tired when I asked this question, apparently I did everything wrong. Can easily be solved by nested v-for loops.

Mock search filter using json-server

I have routes.json and db.json
Route
"/api/*/_search?*=:searchstring": "/$1/?$2_like=:searchstring",
"/api/*": "/$1"
DB.json
{
"cats": {
"cats": []
},
"bats": [],
"recordList": {
"records": [
{id:1, name: 'abc'},
{id:2, name: 'def'},
{id:3, name: 'ghi'}
]
}
}
Absolutely fine fetching the record list with the above configurations.
Need to understand how to mock for the search filter call below:
http:localhost:3001/api/_search?name=abc
Updated the routes to:
{
"/api/*": "/$1",
"/api/_search?name_like": "/$1"
}
Following this link: https://github.com/typicode/json-server/issues/654#issuecomment-339098881
But not hitting the config URL defined, what am I doing wrong? Am I
missing something here? The search term is dynamic, hence the value
passed should be acceptable from a variable only but in the comment it
is static. Kindly assist with this if anyone had similar issues and
resolved
If 'abc' is searched, it should return
{
records: [{id: 1, name: 'abc'}]
}
You need to write your search route like this:
{
"/api/records/_search?name=:searchstring": "/records/?name_like=:searchstring"
}
Or even better, you can parametrize with * to $1 replacement, thus you will be able to search for any parameter in query, and in any dataset, records or other:
{
"/api/*/_search?*=:searchstring": "/$1/?$2_like=:searchstring",
"/api/*": "/$1"
}
Afterwards your request to http://localhost:3001/api/records/_search?name=ab will be with response:
[
{
"id": 1,
"name": "abc"
}
]
Additional docs on routing.

JSON Schema - multiple types

I have this schema. It checks comments, and works fine at the moment.
var schema = {
id: '',
type: 'object',
additionalProperties: false,
properties: {
text: {
type: 'string',
minLength: 1,
required: true
},
author: {
type: 'number',
required: true
}
}
};
My comment structure is:
{
text: "Hello world!",
author: 1
}
But now, I need to validate an array of objects like this. So I can get something like:
[
{
text: "Hello world! Im comment #1",
author: 1
},
{
text: "Super awesome comment #2!",
author: 0
}
]
Sometimes I get one comment only so I get one object, and need to use first schema, but sometimes I get an array of comments, and my schema does not fit.
I heard about json schema anyOf, but I dont know how to do it.
Some like:
anyOf
schema-1 (object)
schema-2 (array with objects)
Any help?
Thanks.
The solution is to have a common definition in one place, and then reference that common definition from two different options inside oneOf:
Here, we put the simple object definition inside definitions:
{
"definitions": {
"singleObject": {
... same definition as in your question ...
}
}
}
We then reference this schema, inside oneOf:
{
"oneOf": [
{"$ref": "#/definitions/singleObject"}, // plain object
{
"type": "array", // array of plain objects
"items": {"$ref": "#/definitions/singleObject"}
}
],
"definitions": {
"singleObject": {...}
}
}
You can organise this a few different ways - I personally often end up with the simple-object definition as the root schema, and have the single/array switcher in definitions, so the schema for my documents is actually http://example.com/schema#/definitions/arrayOrSingle.

Json array in mongoDB

I want to get objects according to an ID they have in an array in a json file in mongodb.
I tried a lot of ways to get them with no success:
db.collection.find({"Id":"2"})
db.collection.find({"Messages.Id":"2"})
db.collection.find({"Messages":{$elemMatch:{"Id":"2"}}})
db.collection.find({"Messages.Id":{$elemMatch:{"Id":"2"}}})
{
"Messages" : [
{
"text":"aaa",
"Id" : [ "1", "2" ]
},
{
"texts" : "bbb",
"Id" : [ "1", "3" ]
}
]
}
Even though that's how it's supposed to be done according to the mongodb documentation.
So I thought something was wrong with my json design (I tried changing it but that didn't help either).
Can anyone suggest to me a good design or query to get the objects with a certain id will work?
UPDATE:
I want for example that if in the query i request the id 2
only the first message and all of it will be displayed (I don't mind if the Id field wont be displayed)
{
"text":"aaa",
"Id":["1","2"]
}
To find single elements that match you will need to utilize the positional operator ($).
db.collection.find({"Messages.Id": "2"}, {"Messages.$": 1, _id: 0})
For finding multiple matches, you would use the aggregation pipeline:
db.collection.aggregate([
{ $unwind: "$Messages" },
{ $match: {"Messages.Id": "1"}},
{ $group: { _id: null, messages: { $push: "$Messages"}}}
])

jqGrid JSON notation on objects

there!
I´ve one column in my jqGrid that is empty.
But i checked the object on chrome console and thats fine.
colModel definition
colModel:[
{name:'id',index:'id', width:55,editable:false,editoptions:{readonly:true,size:10},hidden:true},
{name:'firstName',index:'firstName', width:100,searchoptions: { sopt: ['eq', 'ne', 'cn']}},
{name:'lastName',index:'lastName', width:100,editable:true, editrules:{required:true}, editoptions:{size:10}},
{name:'books[0].nome',index:'books[0].nome', width:100,editable:true, editrules:{required:true}, editoptions:{size:10}},
{"formatter":"myfunction", formatoptions:{baseLinkUrl:'/demo/{firstName}|view-icon'}}
]
JSON response
{
"total": "10",
"page": "1",
"records": "3",
"rows": [
{
"id": 1,
"firstName": "John",
"lastName": "Smith",
"books": [{"nome": "HeadFirst"}]
},
{
"id": 2,
"firstName": "Jane",
"lastName": "Adams",
"books": [{"nome": "DalaiLama"}]
},
{
"id": 35,
"firstName": "Jeff",
"lastName": "Mayer",
"books": [{"nome": "Bobymarley"}]
}
]
}
chrome console inspect object
rowdata.books[0].nome
"HeadFirst"
Any one know where theres are possibles trick?
Tks!
You should use as the value of name property of colModel only the names which can be used as property name in JavaScript and as CSS id names. So the usage of name:'books[0].nome' is not good idea.
To solve your problem you can use jsonmap. For example you can use dotted name conversion:
{name: 'nome', jsonmap: 'books.0.nome', ...
In more complex cases you can use functions as the value of jsonmap. For example
{name: 'nome', jsonmap: function (item) {
return item.books[0].nome;
}, ...
You can find some more code examples about the usage of jsonmap in other old answers: here, here, here, here, here.
name is intended to be a unique name for the row, not a reference to a JSON object. From the jqGrid colModel options documentation:
Set the unique name in the grid for the column. This property is required. As well as other words used as property/event names, the reserved words (which cannot be used for names) include subgrid, cb and rn.
You can also observe how .name is used within grid.base.js - for example:
var nm = {},
...
nm = $t.p.colModel[i].name;
...
res[nm] = $.unformat.call($t,this,{rowId:ind.id, colModel:$t.p.colModel[i]},i);
Anyway, to get back to your question I think you will have better luck by passing down the book name directly - as strings and not objects - and referencing it by name as something like bookName.