Skip to content
This repository has been archived by the owner on Feb 2, 2023. It is now read-only.

Take only first value from list of returned values #67

Open
ronlut opened this issue Jan 30, 2018 · 26 comments · May be fixed by #68
Open

Take only first value from list of returned values #67

ronlut opened this issue Jan 30, 2018 · 26 comments · May be fixed by #68

Comments

@ronlut
Copy link

ronlut commented Jan 30, 2018

Hi.
I am wondering whether it's somehow possible to take only first value (or value at location i) when receiving a list of values in json.
For example, for this output:

"data": [
    {
        "price": 1
    },
    {
        "price": 2
    },
]

I would like to get 1 using something like /data/price/[0].
Is this something currently possible?
Thanks

@tordans
Copy link

tordans commented Jan 30, 2018

@ronlut maybe check out https://gist.github.com/chrislkeller/5719258#gistcomment-2272498 => https://gist.github.com/allenyllee/c764c86ed722417948fc256b7a5077c4 which looks like it allows you to just select specific array items.
This forks might be outdated though since it is based on the gist-version of this script which - from my understanding - came before the github repo.

@ronlut
Copy link
Author

ronlut commented Jan 31, 2018

@tordans Thanks. I'll take a look at it and create a pull request to merge it with this repo :)

@Hobbesball
Copy link

Hobbesball commented Feb 15, 2018

@tordans @ronlut did you get this to work, in the end? I'm trying to query my JSON results in the same way, but still get 'An array value cannot be found' errors.

e.g. my working call is ImportJSON("http://europeana.eu/api/v2/search.json?query="books"&rows=0&start=1&profile=facets&wskey=api2demo", "/facets/fields/label", "noTruncate,noHeaders")
I want to be able to get different rows in the 'fields' array, like so: ImportJSON("http://europeana.eu/api/v2/search.json?query="books"&rows=0&start=1&profile=facets&wskey=api2demo", "/facets/fields[2]/label", "noTruncate,noHeaders"), which should work with @allenyllee's script but doesn't.

@allenyllee
Copy link

allenyllee commented Feb 15, 2018

Hi @Calvinwuyts ,

It seems that the parameter in your function call may have two problems:

  1. The URL "http://europeana.eu/api/v2/search.json?query="books"&rows=0&start=1&profile=facets&wskey=api2demo" seems broken, because the double quote of "books". It'll cause parsing error. Removing the double quote fix this.

  2. The XPath "/facets/fields[2]/label" is incorrect, because "/facets" also need array index. Using "/facets[0]/fields[2]/label" it'll give you label in /facets[0]/fields[2], and similarly, "/facets[1]/fields[2]/label", "/facets[3]/fields[2]/label"... etc.

Below is the fixed code:

ImportJSON("http://europeana.eu/api/v2/search.json?query=books&rows=0&start=1&profile=facets&wskey=api2demo", "/facets[0]/fields[2]/label", "noTruncate,noHeaders")

@Hobbesball
Copy link

hi @allenyllee , thank you so much, that makes a bunch of sense! I got it to work perfectly. Thanks again!

@ronlut ronlut linked a pull request Feb 15, 2018 that will close this issue
@ronlut
Copy link
Author

ronlut commented Feb 15, 2018

@Calvinwuyts Sorry for joining late, you can take a look at @allenyllee's solution integrated into this library by looking at #68 :)
(forgot to update until I saw the message from you)

@Hobbesball
Copy link

hiya @ronlut, thanks for the pull!

@mtompkins
Copy link

mtompkins commented Mar 12, 2018

I believe I'm seeing a sort of similar effect where the api call is returning all results that are equal or a subset of the query which is causing a little difficulty.

Example: http://whattomine.com/coins/214.json returns

{  
   "id":214,
   "name":"BitcoinGold",
   "tag":"BTG",
   "algorithm":"Equihash",
   "block_time":"550.0",
   "block_reward":12.5,
   "block_reward24":12.5,
   "block_reward3":12.5,
   "block_reward7":12.5,
   "last_block":518149,
   "difficulty":3610556.00068795,
   "difficulty24":3143037.1281391,
   "difficulty3":3323764.18655435,
   "difficulty7":3426535.49293562,
   "nethash":53777590,
   "exchange_rate":0.008639,
   "exchange_rate24":0.00864725263157894,
   "exchange_rate3":0.00880546407163743,
   "exchange_rate7":0.00897479941744241,
   "exchange_rate_vol":98.55452268,
   "exchange_rate_curr":"BTC",
   "market_cap":"$1,324,576,111",
   "pool_fee":"0.000000",
   "estimated_rewards":"0.036492",
   "btc_revenue":"0.00031525",
   "revenue":"$2.85",
   "cost":"$0.86",
   "profit":"$1.98",
   "status":"Active",
   "lagging":true,
   "timestamp":1520887718
}

If I try to extract the value at difficulty, the return includes all 4 difficulty* results:

=ImportJSON("http://whattomine.com/coins/214.json", "/difficulty")

returns:

  24 3 7
3610556.00068795 3143037.1281391 3323764.18655435 3426535.49293562

Is it possible to limit the return to the explicit item?

@allenyllee
Copy link

Hi @mtompkins ,
My gist version is OK. It seems that the PR #68 by @ronlut has not yet been merged.

@mtompkins
Copy link

@allenyllee Thank you, however, I do not see this resolving the issue. Perhaps I misunderstand its usage. If I try =ImportJSON("http://whattomine.com/coins/214.json", "/difficulty[0]") it fails.

@allenyllee
Copy link

@mtompkins Try ImportJSON("http://whattomine.com/coins/214.json", "/difficulty")

@mtompkins
Copy link

Your GIST has the same effect - all 4 are returned.

@allenyllee
Copy link

allenyllee commented Mar 13, 2018

@mtompkins No, just one!

@mtompkins
Copy link

@allenyllee
Copy link

Hi @mtompkins, sorry, I can not reproduce this issue....

@mtompkins
Copy link

So it's clearly a Google Sheets issue. I created a totally new document, pasted your GIST and it works as you have described. The old sheet doesn't seem to update the function / script correctly. What a pain!

@loschguy
Copy link

loschguy commented May 4, 2018

Try to get Google map API to sort a list of Costco locations. Thanks

Anyway to get "San Bernardino County" for "types" : [ "administrative_area_level_2", "political" ].
please note that some result set might not contain "types" : [ "administrative_area_level_2", "political" ] if data not available.

{
"results" : [
{
"address_components" : [
{
"long_name" : "13111",
"short_name" : "13111",
"types" : [ "street_number" ]
},
{
"long_name" : "Peyton Drive",
"short_name" : "Peyton Dr",
"types" : [ "route" ]
},
{
"long_name" : "Chino Hills",
"short_name" : "Chino Hills",
"types" : [ "locality", "political" ]
},
{
"long_name" : "San Bernardino County",
"short_name" : "San Bernardino County",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "California",
"short_name" : "CA",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
},
{
"long_name" : "91709",
"short_name" : "91709",
"types" : [ "postal_code" ]
},
{
"long_name" : "6002",
"short_name" : "6002",
"types" : [ "postal_code_suffix" ]
}
],
"formatted_address" : "13111 Peyton Dr, Chino Hills, CA 91709, USA",
"geometry" : {
"location" : {
"lat" : 34.0141463,
"lng" : -117.7424536
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 34.0154952802915,
"lng" : -117.7411046197085
},
"southwest" : {
"lat" : 34.0127973197085,
"lng" : -117.7438025802915
}
}
},
"partial_match" : true,
"place_id" : "ChIJLYJzn2Mtw4ARDSJACuj_XMA",
"types" : [ "street_address" ]
}
],
"status" : "OK"
}

@NickB23
Copy link

NickB23 commented Jul 14, 2018

@allenyllee I'm using your gist https://gist.github.com/allenyllee/c764c86ed722417948fc256b7a5077c4

If I am using your example:
ImportJSON("http://europeana.eu/api/v2/search.json?query=books&rows=0&start=1&profile=facets&wskey=api2demo", "/facets[0]/fields[2]/label", "noHeaders, noTruncate")

My result is Biblionet no matter what I try (tried several accounts, opening different spreadsheets):

image 2018-07-14 at 12 18 43 pm

while the result should have been:
"Bibliothèque nationale de France"

However when I change it to:
ImportJSON("http://europeana.eu/api/v2/search.json?query=books&rows=0&start=1&profile=facets&wskey=api2demo", "/facets[2]/fields[1]/label", "noTruncate,noHeaders")

I do get the correct answer (1688)!

There seems to be a problem when the first argument is [0].

I'm asking because I want to use the Google Maps API. Whenever I use:
=ImportJSON("https://maps.googleapis.com/maps/api/geocode/json?address=New+York&key={API_KEY_HERE}", "/results[0]/address_components[2]/long_name", "noHeaders, noTruncate")

The result is:

image 2018-07-14 at 12 24 26 pm

While it should have been "United States":

image 2018-07-14 at 12 23 36 pm

What am I doing wrong?

@allenyllee
Copy link

allenyllee commented Jul 16, 2018

@NickB23 Thanks for your report. You are right, this is an issue. Here is my fixed code https://gist.github.com/allenyllee/c764c86ed722417948fc256b7a5077c4

// Changelog:
// (Jul. 16 2018) tag: allenyllee-20180716
// 1. Fixed the issue "If you try to query /arrayA[k]/arrayB[n]/arrayC[m]/.../member, you will always get /arrayA[k]/arrayB[k]/arrayC[k]/.../member."

Below is the test example which query from /facets[0]/fields[0]/label to /facets[10]/fields[10]/label

@NickB23
Copy link

NickB23 commented Jul 16, 2018

@allenyllee It's working now! Thanks!

@gitboss
Copy link

gitboss commented Aug 7, 2018

@allenyllee I'm using your gist - https://gist.github.com/allenyllee/c764c86ed722417948fc256b7a5077c4

While using the following example -

=ImportJSON("https://www.reddit.com/r/news/comments/94ssi3/80yearold_medical_marijuana_patient_with_expired/.json?limit=1","/data[0]/children[0]/subreddit","noHeaders")

I expect to get and I get the correct result that is = news

However if I use the following

=ImportJSON("https://www.reddit.com/r/news/comments/94ssi3/80yearold_medical_marijuana_patient_with_expired/.json?limit=1","/data[0]/children[0]/subreddit,/data[0]/children[0]/title","noHeaders")

I expect to get the result = news and 80-year-old medical marijuana patient with expired card jailed for less than an eighth of cannabis

However I the get the entire data under /data[0]/children[0].

What am I doing wrong?

@allenyllee
Copy link

Just input one path at a time, don't use comma separate with multiple path.

So you need to separate your single request into two items: /data[0]/children[0]/subreddit and /data[0]/children[0]/title.

=ImportJSON("https://www.reddit.com/r/news/comments/94ssi3/80yearold_medical_marijuana_patient_with_expired/.json?limit=1","/data[0]/children[0]/subreddit","noHeaders")

=ImportJSON("https://www.reddit.com/r/news/comments/94ssi3/80yearold_medical_marijuana_patient_with_expired/.json?limit=1","/data[0]/children[0]/title","noHeaders")

@dabros
Copy link

dabros commented Dec 23, 2018

Is there any way to use wildcards in pathing, or xpath like syntax "//name"?

#71 (comment)

https://gist.github.com/allenyllee/c764c86ed722417948fc256b7a5077c4#gistcomment-2704820

#91 (comment)

@Duartemartins
Copy link

Hey I have the following Json array:

 "Topc": {
    "all": 1080,
    "all_improved": 73,
    "all_declined": 272,
    "all_difference": 0,
    "all_left": 0,
    "all_entered": 0,
    "all_unchanged": 735,
    "top3": 63,
    "top3_improved": 14,
    "top3_declined": 12,
    "top3_difference": 8,
    "top3_left": 4,
    "top3_entered": 12,

and when filtering by "/Topc/top3" I get all values beginning with top3 when I just want the first. How do I filter out the others?

@allenyllee
Copy link

@Duartemartins your question seems the same as #67 (comment)

Try my gist version: https://gist.github.com/allenyllee/c764c86ed722417948fc256b7a5077c4

@Duartemartins
Copy link

@Duartemartins your question seems the same as #67 (comment)

Try my gist version: https://gist.github.com/allenyllee/c764c86ed722417948fc256b7a5077c4

Thanks! Yes that works if I change the query to "/top3".

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants