Unable to get nested array of objects from json-server - javascript

I am trying to get array of objects from one of the nested JSON properties fields through the json-server, but I am getting 404 error.
When I access the URL in the browser 'http://localhost:4001/templates/5f105f243c076f3a82c95353' I get the JSON as mentioned in the db.json, but when I try to access this URL 'http://localhost:4001/templates/5f105f243c076f3a82c95353/fields' I am getting 404 error.
I believe from the db.json file, as fields array is a part of template so /fields followed by the base URL of the particular template (http://localhost:4001/templates/5f105f243c076f3a82c95353) should work but it is not working, please help me.
db.json file
{
"id": "5f105f243c076f3a82c95353",
"templateIdName": "DNIS Groups",
"templateDisplayName": "DNIS Groups Template",
"templateDescription": "Template for DNIS Groups",
"fieldCounter": 2,
"fields": [
{
"id": 1,
"fieldIdName": "OutageGroup",
"fieldDisplayName": "Outage Group",
"description": "Outage Group",
"type": "MultiList",
"values": [
{
"listValue": "2803",
"listDisplay": "Wire Down"
},
{
"listValue": "2804",
"listDisplay": "Streetlight Outage"
}
],
"initialValue": "2803",
"lockValue": "false",
"requireValue": "true",
"uniqueField": "true",
"autoFilter": "false",
"hideInList": "false",
"indexField": "true"
},
{
"id": 2,
"fieldIdName": "ProtectLinesGroups",
"fieldDisplayName": "Protect Lines Group",
"description": "Protect Lines Group",
"type": "MultiList",
"values": [
{
"listValue": "2814",
"listDisplay": "Cover Lines"
},
{
"listValue": "2815",
"listDisplay": "Voltage"
}
],
"initialValue": "2814",
"lockValue": "false",
"requireValue": "true",
"uniqueField": "false",
"autoFilter": "false",
"hideInList": "false",
"indexField": "false"
}
]
}
routes.json file
{
"/webservice/:clientId/dblookup/template": "/templates",
"/webservice/:clientid/dblookup/template/:id": "/templates/:id",
"/webservice/:clientid/dblookup/template/:id/fields": "/templates/:id/fields"
}

Actually JSON-Server don't have the provision to access the nested resources, it can only access resources which are defined as the top level properties in the file, in this example the top level property will be templates and I can access that using http://localhost:4001/templates but I cannot access the nested property values here as http://localhost:4001/templates/2/values it gives 404 not found error.

Related

Modify mapping of field in Elastic Search

I have a collection named templates. It has a field 'sec_1' which is an array of objects with properties 'name' and 'des' both of type string. Now I'm using elastic search and when I try to execute a query that matches only title fields I get an error saying the sec_1 field is not nested. Upon checking the mappings here is what I get mapping type of sec_1 is text.
I checked online and saw the fix was to modify the mapping. I created a new index like:
`curl base_url/new_index_name
{
"mappings":
{"properties": {
"sec_1": {
"type":
"nested","properties": {
"name": { "type": "text" },
"des": { "type": "text" }
}
}
}
}
}
and then reindexed like this:
curl base_url/_reindex
{
"source": {
"index": "old_index"
},
"dest": {
"index": "new_index"
}
}`
First request is successful, the second one fails with this error:
{
"index": "new_index",
"type": "_doc",
"id": "be5123a4-d0e8-4d7b-a8f1-42f31d37fe55",
"cause": {
"type": "mapper_parsing_exception",
"reason": "object mapping for [sec_1] tried to parse field [null] as object, but found a concrete value"
},
"status": 400
},
I don't understand why this is happening and what I'm doing wrong. Please help. I do not know much about elastic search, I've looked for solutions online, tried chatgpt too but the same steps appear. I'm finding the reason for this error is that sec_1 is not nested type but I've checked in database it is an array of objects. What else could be wrong?
I tried creaing new index but cannot do that and without new index my search query cant function properly

check if item exists in json file then get it if exists. And if not complete executing the code

I get a json file from outer link by ajax call. i want to check element is exists in this json file or not and get it if exists. because it changes every time and items increases or decreases. If I get item and this item not existing, I get error and the code not complete. i want to check this item json.children[1].children and json.children[2].children ,..... if exists.
{
"num": 1,
"name": "aa",
"properites": [
{
"name": "prop1",
"value": "value1"
}
],
"children": [
{
"num2": 1,
"name2": "name2",
"properites": [
{
"name": "name",
"value": "value"
}
],
"children": []
},
{
"name": 1,
"num": "1",
"attributes": [
{
"name": "name",
"value": "value"
}
],
"children": []
.......................... big json file
I have tried to get these Items and set in local storage to use in table by this:
localStorage.setItem("row1-item1", json.children[1].children);
localStorage.setItem("row1-item2", json.children[2].children);
localStorage.setItem("row1-item3", json.children[3].children);
localStorage.setItem("row1-item4", json.children[4].children);
localStorage.setItem("row2-item1", json.name[1].children);
localStorage.setItem("row2-item2", json.name[2].children);
localStorage.setItem("row2-item3", json.name[3].children);
localStorage.setItem("row2-item4", json.name[4].children);
the problem here is if json.children[4].children not existing the code not complete the next line and jump out of ajax call even if json.name[1].children is exists. I tried try/catch but not solved it
Try to use map
json.children.map((children,index)=>{
localStorage.setItem('row${index}-item${index}`, children);
}
Or you can use forEach or for loop instead of map.

Get the list of users that liked a post

A friend of mine decided to run a sweepstakes on Instagram without giving much thought to how to get the full list of users that liked a post. He's saying he can just pick a random user from the available/visible list, but that wouldn't be fair, so I decided to step in.
The post in question, currently, has 1.4k likes.
First, I created a tiny script in JS to view, scroll down (to populate the list) and finally get the users that liked the post. This was troublesome to create, but it works. Then I realized I could not view all the users. While there are 1.4k users, I could only list 675 of them.
In the name of fairness, this is not enough. So I started digging more and examined the HTTP Requests made by the (Instagram) post page to the Instagram Graph API to load more users. This is what the URL looks like:
https://www.instagram.com/graphql/query/?query_id=SOME_ID&variables={\"shortcode\":\"SHORTCODE\",\"first\":20}
When I make a request to this URL, I get the following:
{
"data": {
"shortcode_media": {
"id": "SOME_ID",
"shortcode": "SHORTCODE",
"edge_liked_by": {
"count": 675,
"page_info": {
"has_next_page": false,
"end_cursor": "AQBPkM1xm2XgBx8ZQ8lR6GDsFAvQBx_Eqxg2NnTXN-GUPGhlpUa9_10UoMcJ6xNcIH4"
},
"edges": [{
"node": {
"id": "", // value omitted intentionally
"username": "", // value omitted intentionally
"full_name": "", // value omitted intentionally
"profile_pic_url": "", // value omitted intentionally
"is_verified": false,
"followed_by_viewer": false,
"requested_by_viewer": false
}
},
...
There's this ["edge_liked_by"]["count"] property and it is set to 675. I'm guessing this is a server-side restriction. When I increase the "first" parameter in the URL and make it greater than 675, it still returns 675 users.
Can I overcome this restriction and get the full list in any way?
Update: I've just tried the same thing with comments. The post has 11.8k comments and this is what the request returns:
{
"data": {
"shortcode_media": {
"edge_media_to_comment": {
"count": 11809,
"page_info": {
"has_next_page": true,
"end_cursor": "AQBV53OxNFkaHwJ6xjgHmlI-hwtpHCEeButMmGLwZJ_sjdyUy49gY_WZo1iH_aRcuAFOCzfrKPEktMaQLRjFVAsmQTincJpr4ZTITbTT1BZkJQ"
},
"edges": [{
"node": {
"id": "",
"text": "",
"created_at": ,
"owner": {
"id": "",
"profile_pic_url": "",
"username": ""
}
}
},
...
The exact count of the comments is 11809 and the request listed first 10259, but there's a next page. So I requested the next page and this is it:
{
"data": {
"shortcode_media": {
"edge_media_to_comment": {
"count": 11809,
"page_info": {
"has_next_page": false,
"end_cursor": null
},
"edges": [{
"node": {
"id": "",
"text": "",
"created_at": ,
"owner": {
"id": "",
"profile_pic_url": "",
"username": ""
}
}
},
...
This time it didn't return the remaining 1550 (11809 - 10259) users, but just 356 of them. And there's no next page. So there seems to be some inconsistencies. (Perhaps, there's a privacy issue that prevents some users to be listed?)

How do I access values in a d3 json file with nested array objects

Previously I was using a json file with the following format:
[{"lat":43.788458853157117,"lng":-79.282781549043008,"category":"volunteer","name":"Rita","url":"", "description":"xxx is a member of 13"},{"lat":43.7,"lng":-79.4,"category":"organization","name":"TCAN","url":"http://tcan.ca","description":"Lorem ipsum"}]
Now I am attempting to generate the json file from a Drupal site and am getting the following structure. How can I reference the lowest level fields. I have looked at examples using d3.net but have not found any that apply.
{
"organizations": [
{
"member": {
"field_category": "organization",
"body": "A network of organizations in Toronto devoted to climate change mitigation and adaptation.",
"URL": "xxx.ca",
"title": "Toronto Climate Action Network",
"field_lat": 43.7,
"field_long": -79.4
}
},
{
"member": {
"field_category": "abc",
"body": "xxx.",
"URL": "",
"title": "yyy",
"field_lat": 43.7,
"field_long": -79.28
}
}
]
}
Assuming that your data is stored in the variable data:
var bottom = data.organizations.map(function(d) { return d.member; });

JSON parsing in JS

I am getting a JSON in response from server:
{
"width": "765",
"height": "990",
"srcPath": "http://192.168.5.13:8888/ebook/user_content/_ADMIN_/_MERGED_/1273.pdf",
"coverPage": "",
"documents": [
{
"index": "1",
"text": "Archiving Microsoft® Office SharePoint® Server 2007 Data with the Hitachi Content Archive Platform and Hitachi Data Discovery for Microsoft SharePoint",
"type": "doc",
"id": "HDS_054227~201106290029",
"children": [
{
"text": "Page 1",
"leaf": "true",
"pageLocation": "http://192.168.5.13:8888/ebook/user_content/_ADMIN_/_IMAGES_/HDS_054227~201106290029/image_1.png"
},
{
"text": "Page 2",
"leaf": "true",
"pageLocation": "http://192.168.5.13:8888/ebook/user_content/_ADMIN_/_IMAGES_/HDS_054227~201106290029/image_2.png"
}
]
},
{
"index": "11",
"text": "Brocade FCoE Enabling Server I/O Consolidation",
"type": "doc",
"id": "HDS_053732~201105261741",
"children": [
{
"text": "Page 1",
"leaf": "true",
"pageLocation": "http://192.168.5.13:8888/ebook/user_content/_ADMIN_/_IMAGES_/HDS_053732~201105261741/image_1.png"
},
{
"text": "Page 2",
"leaf": "true",
"pageLocation": "http://192.168.5.13:8888/ebook/user_content/_ADMIN_/_IMAGES_/HDS_053732~201105261741/image_2.png"
}
]
}
]
}
And I want to get pagelocation of the children.
Can anyone tell me how to do this?
Hi
i also want to get indexes from this and then want to get pagelocations of that particular children. Can you tell me how would i do that?
And also when i when i am getting indexes array it is returning me ,, only and not the index nos.
I am using following code for that :
indexes=response.documents.map(function(e){ return e.children.index; })
Thanks & Regards
If you're interested in simply retrieving all the page locations, you can do it using filter:
var locations = [];
json.documents.forEach(function(e,i) {
e.children.forEach(function(e2,i2) {
locations.push(e2.pageLocation);
)}
});
// returns flat array like [item1,item2,item3,item4]
You can get an array of arrays using map:
var locations = [];
var locations = json.documents.map(function(e) {
return e.children.map(function(e2) {
return e2.pageLocation;
});
});
// returns 2-dimensional array like [[item1,item2],[item1,item2]]
Your json response is an appropriate javascript object So you can access all elements of the object like you do as in back end.
here, you have an array of object of the type documents and each document object has array of objects of the type children. so
syntax would be
myjson.documents[0].children[0].pagelocation
( = http://192.168.5.13:8888/ebook/user_content/_ADMIN_/_IMAGES_/HDS_054227~201106290029/image_1.png)
will give you the very first page location..
and so on

Categories

Resources