kI have a problem with the library https://github.com/JSONPath-Plus/JSONPath at the latest version.
Example:
{
"firstName": "John",
"lastName": "doe",
"age": 26,
"address": {
"streetAddress": "naist street",
"city": "Nara",
"postalCode": "630-0192"
},
"phoneNumbers [1]": [
{
"type": "iPhone",
"number": "0123-4567-8888"
},
{
"type": "home",
"number": "0123-4567-8910"
}
]
}
Now i want to get "phoneNumbers [1]" with
path: $..['phoneNumbers [1]']
Here is a Demo Page for testing: https://jsonpath-plus.github.io/JSONPath/demo/
That did not work because of the square brackets inside of the String.
Do anyone know how to solve this Problem.
I have a lot of Json Objects containing strings with square brackets, so if there is a solution not only for a specific case.
Is there any option to escape characters so that the problem will be solved?
The json Object above is only a Example Code to describe the Problem.
The Problem also occurs when only using JSONPath so it is not limited to that Library.
https://jsonpath.com/
This appears to be a bug in JSONPath-Plus. The jsonpath library can handle it.
const data = JSON.parse(`{
"firstName": "John",
"lastName": "doe",
"age": 26,
"address": {
"streetAddress": "naist street",
"city": "Nara",
"postalCode": "630-0192"
},
"phoneNumbers [1]": [
{
"type": "iPhone",
"number": "0123-4567-8888"
},
{
"type": "home",
"number": "0123-4567-8910"
}
]
}`);
const result = jsonpath.query(data, "$..['phoneNumbers [1]']");
console.log(result);
<script src="https://unpkg.com/jsonpath#1.1.1/jsonpath.js"></script>
There's not much you can do about this beyond:
Changing your data structure
Changing your library
Filing a bug report
There is a workaround you can use only with jsonpath-plus
$[?(#property == "phoneNumbers [1]")]
Related
I just need to retrieve an object by its id from this JSON object (peopleData). When I do typeof peopleData, it returns object. When I try to do typeof peopleData[0] (treating this object as an array because it has brackets as the opening and closing elements) it returns undefined. How do I loop through all of the objects to find the appropriate object if this JSON object is an array that returns "undefined" when I try to get a specific element of that array? I don't know how to enumerate over or de-structure this object since it is an array.
Here are the first few lines from the JSON that I was given (I changed the values but that is irrelevant. I am mainly emphasizing the bracket at the beginning). This is the raw JSON object that I copied from its raw form on GitHub:
[{
"id": "237856238235",
"ip_address": "3423423.42.42.4",
"ssn": "123133231",
"date_of_birth": "3123123",
"address": {
"home": {
"street_number": "231231",
"street_name": "ef2ef23",
"street_suffix": "8i6rth2",
"city": "wefwdfwef"
"state": "affwefwfww",
"zip": "wefsdfbghyj"
},
"work": {
"street_number": "wefgwegwe",
"street_name": "wefwefwf",
"street_suffix": "wsfaf",
"city": "aefaef",
"state": "afaef",
"zip": "aefaef"
}
}
}, {
"id": "fwefewf",
"ip_address": "fwefwf",
"ssn": "wfwef",
"date_of_birth": "wefwef",
"address": {
"home": {
"street_number": "efwef",
I have done this before with this exact data and it worked when I just used a small function that cycles through each element of the array, but I have no idea why it is not working now.
I diagnosed my problem incorrectly. It did not have to do with the JSON data. I forgot to use await when getting the data with Axios. Thanks so much for any effort you put in to help me with this.
You can use filter by the id you want.
const data = [{
"id": "237856238235",
"ip_address": "3423423.42.42.4",
"ssn": "123133231",
"date_of_birth": "3123123",
"address": {
"home": {
"street_number": "231231",
"street_name": "ef2ef23",
"street_suffix": "8i6rth2",
"city": "wefwdfwef",
"state": "affwefwfww",
"zip": "wefsdfbghyj"
},
"work": {
"street_number": "wefgwegwe",
"street_name": "wefwefwf",
"street_suffix": "wsfaf",
"city": "aefaef",
"state": "afaef",
"zip": "aefaef"
}
}
},
{
"id": "fwefewf",
"ip_address": "fwefwf",
"ssn": "wfwef",
"date_of_birth": "wefwef",
"address": {
"home": {
"street_number": "efwef",
"street_name": "ef2ef23",
"street_suffix": "8i6rth2",
"city": "wefwdfwef",
"state": "affwefwfww",
"zip": "wefsdfbghyj"
},
"work": {
"street_number": "wefgwegwe",
"street_name": "wefwefwf",
"street_suffix": "wsfaf",
"city": "aefaef",
"state": "afaef",
"zip": "aefaef"
}
}
}]
let result = data.filter(obj => obj.id === "237856238235")
//console.log(result)
console.log(result[0])
I am writing a script in Nodejs to map a json to mongodb but I am really confused on how to tackle this as the structure is really complex. Any help would be appreciated! So basically the structure looks like this.
{
"4.1": {
"picture": "",
"code": {
"PEK": {
"number": 4.1,
"text": "XYZ."
},
"DXB": {
"number": 5.1,
"text": "XYZ"
},
"LAX": {
"number": 6.1,
"text": "XYZ"
},
"HND": {
"number": 6.1,
"text": "XYZ"
}
}
},
"4.2": {
"picture": "",
"province": {
"PEK": {
"number": 4.2,
"text": "XYZ"
},
"DXB": {
"number": 5.2,
"text": "XYZ"
},
"LAX": {
"number": 6.2,
"text": "XYZ"
},
"HND": {
"number": 6.2,
"text": "XYZ"
}
}
}
}
So I have put two instances for example just so it's easier to understand. What I want is that I am using the the number of "PEK" as a key to access the right object. When I get response from the server I get a number, and the code, for example 4.1 and DXB. Using this information I want to show DXB under 4.1 and also its respective image. All the objects under 4.1 are tied to a common image which is linked to 4.1 but they have their own respective number and text which I want to show at runtime depending on the response. What would be the smartest way to map this to mongodb. Any help would be appreciated, thanks!!
I have a code snippet of JavaScript:
mf.profile.push({
"Site": {
"Name": "Jack Montana",
"Identity": 61026032,
"Email": "jack#gmail.com",
"Phone": "+14155551234",
"Gender": "M",
}
});
mf.event.push("Product viewed", {
"Product name": "Casio Chronograph Watch",
"Category": "Mens Accessories",
"Price": 59.99,
"Date": new Date()
});
Now my question is what does mf.profile.push or mf.event.push signify?
Is mf an object and profile a function? or both are classes and push is a function?
what does mf.profile.push or mf.event.push signify
The mf variable is an Object literal and the profile/event are properties (may of type array) belongs to this object.
The push() is a method that will add the items passed as parameters to those attributes.
I have following JSON data but I don't know how to iterate through it and read its all values:
var students = {
"student1": {
"first_name": "John",
"last_name": "Smith",
"age": 24,
"subject": [{
"name": "IT",
"marks": 85
},
{
"name": "Maths",
"marks": 75
},
{
"name": "English",
"marks": 60
}
]
},
"student2": {
"first_name": "David",
"last_name": "Silva",
"age": 22,
"subject": [{
"name": "IT",
"marks": 85
},
{
"name": "Maths",
"marks": 75
},
{
"name": "English",
"marks": 60
}
]
}
};
I would like to use following methods to do the needful:
Using for in loop
Using simple for loop
Using $.each in jQuery
I will prefer to display above values in <ul> <li> nicely formatted.
Also, please suggest me what will be look of above JSON data if I put it in an external .json file?
You can use for in loop to iterate over the object, as it iterates over the properties of an object in an arbitrary order, and needs to use .hasOwnProperty, unless inherited properties want to be shown.
Now about accessing the object, let's say I have a JSON like
var myJson={name:"john",age:22,email:"email#domain.com"};
and I need to access the value of name i would simply use . operator using the myJson variable i.e console.log(myJson.name) will output john. because it will be treated as an object, now if I make a little change and make the object like below
var myJson=[{name:"john",age:22,email:"email#domain.com"}];
now if you try to access the value of the property name with the same statement above you will get undefined because the [] will now treat it as an object(JSON) with an array of 1 person or a JSON Array, now if you access it like console.log(myJson[0].name) it will print john in console what if there was more than one person in the array? then it will look like following
var myJson=[
{name:"john",age:22,email:"john#domain.com"},
{name:"nash",age:25,email:"nash#domain.com"}
];
console.log(myJson[0].name) will print john and console.log(myJson[1].name) will print nash so as I mentioned in the start that you should use for in loop for iterating over an object and if we want to print all the names of the person in the JSON Array it will be like.
for(var person in myJson){
console.log(myJson[person].name, myJson[person].age, myJson[person].email);
}
it will output in the console like below
john, 22, john#domain.com
nash, 25, nash#domain.com
I have tried to keep it simple so that you understand you can look into for in and hasOwnProperty, in your case you have a nested object in which property/key subject is an array so if I want to access the first_name of student1 i will write students.student1.first_name and if I want to print the name of the first subject of student1 I will write students.student1.subject[0].name
Below is a sample script to print all the students along with their subjects and marks and personal information since you JSON is nested I am using a nested for in, although Nested iterations are not necessarily a bad thing, even many well-known algorithms rely on them. But you have to be extremely cautious what you execute in the in the nested loops.
For the sake of understanding and keeping the given example of json object, i am using the same to make a snippet. Hope it helps you out
var students = {
"student1": {
"first_name": "John",
"last_name": "Smith",
"age": 24,
"subject": [{
"name": "IT",
"marks": 85
},
{
"name": "Maths",
"marks": 75
},
{
"name": "English",
"marks": 60
}
]
},
"student2": {
"first_name": "David",
"last_name": "Silva",
"age": 22,
"subject": [{
"name": "IT",
"marks": 85
},
{
"name": "Maths",
"marks": 75
},
{
"name": "English",
"marks": 60
}
]
}
};
$("#print").on('click', function() {
for (var student in students) {
console.log(students[student].first_name + '-' + students[student].last_name);
for (var subject in students[student].subject) {
console.log(students[student].subject[subject].name, students[student].subject[subject].marks);
}
}
setTimeout('console.clear()', 5000);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="button" id="print" value="print-now">
As a complete newbie to the concept of NoSQL it is giving me a hard time figuring out how to achieve something like a join with multiple linked documents.
I got a Data-Structure for a Congress in a CouchDB which is replicated to a PouchDB on a Client like:
Sessions:
"_id": "1",
"type": "session",
"Title": "Title of Session",
}
Persons:
"_id": "2",
"type": "Person",
"mail": "mail#mail.com",
"Names": {
"FirstName": "Horst",
"LastName": "Muller"
}
"_id": "3",
"type": "Person",
"mail": "mail2#mail.com",
"Names": {
"FirstName": "Mark",
"LastName": "Webber"
}
Talks:
"_id": "4",
"type": "presentation",
"Sessions": [
"1"
],
"PresentationTitle": "Title of Talk",
"files": [
{
"Filename": "presentationfile.pptx",
}
],
"Speakers": [
"2",
"3"
]
What I tried to achieve was an output where the documents are retrieved as if putting the doc of the linked document in the result like
"_id": "4",
"type": "presentation",
"Sessions": [
"Title of Session"
],
"PresentationTitle": "Title of Talk",
"files": [
{
"Filename": "presentationfile.pptx",
}
],
"Speakers": [
{
"FirstName": "Mark",
"LastName": "Webber"
},
{
"FirstName": "Horst",
"LastName": "Muller"
}
]
or something similar without denormalizing the data. I tried with map/reduce, Mango and pouchdb-find, but without luck.
Is that even possible in a NoSQL-World?
There is actually a really neat feature in CouchDB called Linked Documents which allows you to effectively do joins.
The gist is that if you emit an object with a _id property as a value, then the behavior of include_docs=true will fetch the linked document instead of the source document. (the docs I linked to above have a detailed example of this)