Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
How to "query" a json file on server with javascript?
Filtering on server side, with ajax request.
{"employees":[
{"firstName":"John", "lastName":"Doe"},
{"firstName":"Anna", "lastName":"Smith"},
{"firstName":"Peter", "lastName":"Jones"}
]}
Something like "select * from emplyees like 'J%'. Shold avoid using db. Just use simple json file and be able to get only filteted data from server.
Need a simple, fast way to get obly needed data, using json file instead of db. Without need to install database
Thanks in advance, best regards
Massimo
it will work http://www.jsonquerytool.com/
{
"key" : "value",
"array" : [
{ "key" : 1 },
{ "key" : 2, "dictionary": {
"a": "Apple",
"b": "Butterfly",
"c": "Cat",
"d": "Dog"
} },
{ "key" : 3 }
]
}
$.array[?(#.key=2)].dictionary.b
RESULT
[
"Butterfly"
]
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 months ago.
Improve this question
The server sends me a data as JSON to descripe a product. A product consists of properties and nested arrays of properties, up to 4 level deep. In the Frontend the user can update deep nested values. Do I need to keep track of the path an then reconstruct the whole JSON object for my updating POST call? Or is there a more elegant way to do it? Or is the design of the backend service bad?
data as JSON
{
"productId": 10,
"features": [
{
"id": 45,
"name": "Customization",
"productOptions": [
{
"id": 1,
"color": ["red", "green"],
},
{
"id": 2,
"slogans": [
{"id": 32, "name":"Thank You"},
{"id": 33, "name":"Thanks a lot"},
],
}
]
}
]
}
input to edit slogan
<input data-id="32" type="text" name="slogan" />
edit JSON to send back to the server
const update = {"id": 32, "name":"Thank You so much!"}
// update slogan array
solgansUpdateIndex = slogans.findIndex(obj => obj.id == 32)
slogans[solgansUpdateIndex] = update
...
// update whole object for the updating POST call
{...data, //updateTree}
You should not need to send the whole object back to the server. What you should do is send a set of ids that uniquely identify which field should be updated.
Example: Let's take your data where productId is 10 and featureId is 45
You can create a controller in the backend such as updateProductSlogan(int productId, int featureId, int sloganId, String/Slogan newSlogan).
And then what this endpoint should do is get the corresponding slogan from your database with a query, using these ids and update that slogan with value of newSlogan param.
Note: If only field that contains slogans is the features field you can also hardcode that and change the method to updateProductSlogan(int productId, int sloganId, String/Slogan newSlogan). in which you will fetch the features property with no need to use an id for.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 10 months ago.
Improve this question
Hey Guys I am exporting some data.
And the challenge I am facing is the data should be in Array of Objects but don't know the data format is Array in Array of Objects.
I can write a script to transform the data to the desired format.
Can anyone help me out with how can I remove the object from the inner array to the outer array?
The data is in this format.
{
"data": [
[
{
"id": "4101"
}
]
]
}
My Resultant Format looks like this
Can anyone help me write a javascript to accomplish this objective
{
"data": [
{
"id": "4104",
}
]
}
Please give a try to this :
var a=obj.data.splice(0, 1);
obj.data.push(a[0]);
or in one line if you want :
obj.data.push(obj.data.splice(0, 1)[0]);
Provided you cannot correct the input at the source.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
first of all, I really haven't learn any javascript at all, and I am stuck here.
I want to get these link in this JSON example.
"list": [
[
"List of recipes",
[
{
"name": "Eggs",
"links": [
{
"method": "Fry",
"link": "https://example.com/category/fry"
},
]
},
]
]
I can't elaborate with my friend's example code, can anyone help me?
Your JSON isn't valid so you need to fix that first.
When it's valid parse it with JSON.parse.
When it's parsed you'll have a nested array data structure. You need to access the links array by navigating through the tree with the array indexes.
// Returns an array
data.list[0]
// Get the second element of that array
// (the first element is the text "List of recipes")
// which is also an array
data.list[0][1]
// Access the first element of that array
// (an object) and return the links array
data.list[0][1][0].links
const json = '{"list":[["List of recipes",[{"name":"Eggs","links":[{"method":"Fry","link":"https://example.com/category/fry"}]}]]]}';
const data = JSON.parse(json);
console.log(data.list[0][1][0].links);
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I'm trying to get the length of an an array of objects from the back-end by using API call.
Here is what a "zone" objective looks like:
[
{
"id": 1,
"name": "a"
},
{
"id": 2,
"name": "b"
},
{
"id": 3,
"name": "c"
},
{
"id": 4,
"name": "d"
}
]
and the following typescript code doesn't return the length correctly:
var lengthZones= Object.keys(this.swimbandsService.getZones()).length;
What is the correct way to get the length of an an array of objects in typescript?
I'm guessing you need to wait for the Promise to resolve first, then you can access the response and count the objects inside the array.
this.swimbandsService.getZones().then((response) => {
// might need to parse the response as json, depending on your service logic
var lengthZones = response.length;
});
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Hello I have a json in javascript like this:
{"data":[{"stuff":[
{"onetype1":[
{"id":1,"name":"John Doe"},
{"id":2,"name":"Don Joeh"}
]},
{"othertype2":[
{"id":2,"company":"ACME"}
]}]
},{"otherstuff3":[
{"thing":
[[1,42],[2,2]]
}]
}]}
I want to know how to calculate in javascript how many 'onetype' tags there are. In php I can request it with count but in javascript I tried with data.lenght and it did not work. Any idea how to do it?
Thank you very much
I think you're looking for Object.keys(data).length. There's a shim for older browsers here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys#Polyfill.
BTW there's no such thing as a "json tag" - the word "tag" the way you're using it applies only to HTML/XML.
This is your nested object
var data = [{"stuff":[
{"onetype1":[
{"id":1,"name":"John Doe"},
{"id":2,"name":"Don Joeh"}
]},
{"othertype2":[
{"id":2,"company":"ACME"}
]}]
},{"otherstuff3":[
{"thing":
[[1,42],[2,2]]
}]
}]
This is how you access lengths and elements
data.length // 2
data[0].stuff.length // 2
data[0].stuff[0].onetype1.length // 2
data[0].stuff[0].onetype1[0] // Object {id: 1, name: "John Doe"}
data[1].otherstuff3.length // 1
data[1].otherstuff3[0].thing.length // 2
data[1].otherstuff3[0].thing[0] // [1, 42]