For example, assuming I can generate this page but won't be able to hard-code the article number, how can I access the thumbnail link in this entry?
https://en.wikipedia.org/w/api.php?action=query&titles=Arcadius&prop=pageimages&format=json&origin=*&pithumbsize=100
assuming that json is a name of your json object, you can try this
var thumbnail = json["query"]["pages"]["2180"]["thumbnail"];
// or thumbnail source
var source = json["query"]["pages"]["2180"]["thumbnail"]["source"];
if you have a json string , not an object you will have to parse it at first
var json=JSON.Parse(jsonString);
Related
When I run the below code in my react-native project
console.log("Response= "+JSON.stringify(response));
I can get output like below in my console.
Response= {"assets":[{"height":3888,"uri":"file:///data/user/0/com.facebook2/cache/rn_image_picker_lib_temp_6b8db334-4fcc-40ba-94a0-325191a89011.jpg","width":5184,"fileName":"rn_image_picker_lib_temp_6b8db334-4fcc-40ba-94a0-325191a89011.jpg","type":"image/jpeg","fileSize":1914937}]}
How I print the 'uri' from that JSON response ?
Looking at your data we can break it down by what we see. So, with JSON we have a Javascript Object which contains a param of assets. So to print assets we would console.log(response.assets)
Assets is an array with one item, so we want to get the first item from that which would be console.log(response.assets[0]).
Then we want the uri from that first assets object which would be console.log(response.assets[0].uri)
Hope this is what you are looking for.
You can use Dot Notation to access the properties of an object.
In your response json, it is seen that it has an array with name assets. The required property uri is inside the array. You can access it simply by
response.assets[0].uri
if there were multiple items in your assets array, you can simply loop over the array and get the values,
const length = response.assets.length;
for(let i=0; i< length; i++)
console.log('URI is = ', response.assets[i].uri)
const response = {"assets":[{"height":3888,"uri":"file:///data/user/0/com.facebook2/cache/rn_image_picker_lib_temp_6b8db334-4fcc-40ba-94a0-325191a89011.jpg","width":5184,"fileName":"rn_image_picker_lib_temp_6b8db334-4fcc-40ba-94a0-325191a89011.jpg","type":"image/jpeg","fileSize":1914937}]};
console.log('URI =', response.assets[0].uri)
I have a data object from the data api that contains an array so that if data object 1 is empty it will move the data index to the same object
example when I set string to pref
prefs.setString('first_name', response['data']['first_name']);
here i try to make list
// Code For List
prefs.setStringList('amount', response['data_finance']['amount']);
API DATA OBJECT // p_co_id , code , description , amount
//
I was trying to use the method found here (see most up-voted answer):
Google Apps Script Fastest way to find a row?
I currently use this while it does work I wanted to try the above linked method yet when I replace the below code
function AutoPopulate (evalue)
{
//uses google drive file irretator reads in JSON file and parses it to a Javascript object that we can work with
var iter = DriveApp.getFilesByName("units.json");
// iterate through all the files named units.json
while (iter.hasNext()) {
// define a File object variable and set the Media Tyep
var file = iter.next();
var jsonFile = file.getBlob().getDataAsString();
// log the contents of the file
//Logger.log(jsonFile);
}
var UnitDatabase = JSON.parse(jsonFile);
//Logger.log(UnitDatabase);
//Logger.log(UnitDatabase[1027]);
return UnitDatabase[evalue];
}
WITH THIS CODE:
function AutoPopulate (evalue)
{
//this method did not work for me but should have according to stackflow answer linked above I am trying to understand why or how I can find out why it may have thrown an error
var jsonFile = DriveApp.getFilesByName("units.json").next(),
UnitDatabase = UnitDatabase.getBlob().getDataAsString();
return UnitDatabase[evalue];
}
I get an error in the excecution indicating that there is a % at postion 0 in the JSON, between the methods I dont alter the JSON file in anyway so I dont understand why does the top method work but the bottom one does not?
For further information the idea behind the code is that I have a list of Unit numbers and model numbers that are in a spreadsheet. I then convert this to a JSON file, this however is only done when a new unit is added to the fleet. As I learned one can parse a whole JSON file into a javascript object which makes working with the data set much faster. This javascript object is used so that when a user enters a UNIT# the MODEL# is auto populated based on the JSON file.
I cannot share the JSON file as it contains client information.
Your code does not work for two reasons:
You have a typo in the line UnitDatabase = UnitDatabase.getBlob()... - it should be UnitDatabase = jsonFile.getBlob()...
If you want to retrieve a nested object from a json file - you need to parse the JSOn - otherwise it is considered a string and you can not access the nested structure
Modified working code:
function AutoPopulate2 (evalue)
{
var jsonFile = DriveApp.getFilesByName("units.json").next();
var UnitDatabase = JSON.parse(jsonFile.getBlob().getDataAsString());
return UnitDatabase[evalue];
}
Mind that this code will only work if you have a "units.json" file on your drive and if evalue is a valid 1st-level nested object of this json.
I want to store the result from a call to a Domino Access Service (DAS) in a localStorage however when I try to convert the result object to a JSON string I get an error.
With DAS you get the result as an Array e.g.:
[
{
"#entryid":"1-CD90722966A36D758025725800726168",
"#noteid":"16B46",
Does anyone know how I get rid of the square brackets or convert the Array quickly to a JSON object?
Here is a snippet of my code:
var REST = "./myREST.xsp/notesView";
$.getJSON(REST,function(data){
if(localStorage){
localStorage.setItem('myCatalog',JSON.stringify(data));
}
});
Brackets are part of the JSON syntax. They indicate that this is an array of objects. And as you point to a view it is very likely that you would get more than one object back (one for each entry in the view).
So if you are only interested in the first element you could do this:
var REST = "./myREST.xsp/notesView";
$.getJSON(REST,function(data){
if(localStorage){
var firstRecord = data[0] || {};
localStorage.setItem('myCatalog',JSON.stringify(firstRecord));
}
});
Otherwise, you would need to define a loop to handle each of the objects :-)
/John
my JSON response is as given below
JSON response is
{"code":201,"message":[["TEST Action","NA","30-11--2011"],["TEST Action 2","NA","30-11--2011"]]}.
i want to take the data correspond to 'message'.i used JSON Array.and got response as
JSON array response is
[["TEST Action","NA","30-11--2011"],["TEST Action 2","NA","30-11--2011"]].
Now how can i access each array in that?
You should expand on what you have done, what language you are using, etc. Normally, you should be able to index into the array with the standard notation. In python for example you can do something along the lines of json_data["message"][0] to access the first array and json_data["message"][1] to access the second.
something like :
var d = JSON.parse('{"code":201,"message":[["TEST Action","NA","30-11--2011"],["TEST Action 2","NA","30-11--2011"]]}')
and then you can access each array in message part as :
d.message.forEach(function(obj) { console.log(obj); });