I have a json string where I need to extract an array.
a='{"player":{"username":"user1","partner_Name":[{"firstname":"my_first"},{"lastname":"my_last"}],"characteristics":{"race":"Human","class":"Warlock","subclass":"Dawnblade","power":300,"playercountry":"USA"}}}'
JsonExtractScalar(a, '$.player.username') It returns user1
But I do JsonExtractScalar(a, '$.player.player_Name') it returns null.
Meanwhile JsonExtract(a, '$.player.username') this returns [{"firstname":"my_first"},{"lastname":"my_last"}]
I want to understand what is the difference between them and when to use which one?
https://docs.aws.amazon.com/athena/latest/ug/extracting-data-from-JSON.html
To extract the scalar value from the JSON string, use the json_extract_scalar function. It is similar to json_extract, but returns only scalar values (Boolean, number, or string).
Note: Do not use the json_extract_scalar function on arrays, maps, or structs.
If you want to extract to an array then simply use
JSON.parse(a);
no need to do extra efforts.
Related
So, my problem is, that in MongoDB that .aggregate() function is not working
Heres some pictures:
You can see what a tried on the first picture
When using "$lookup", it's important that both "localField" values and "foreignField" values have the same type.
In this case, "localField" is a string and "foreignField" is an ObjectId.
It's probably best for queries to use ObjectId's for both.
Assumptions: I have a Julia DataFrame with a column titled article_id.
Normally, I can declare a DataFrame using some syntax like df = DataFrame(CSV.File(dataFileName; delim = ",")). If I wanted to get the column pertaining to a known attribute, I could do something like df.article_id. I could also index that specific column by doing df."article_id".
However, if I created a string and assigned it to the value of article_id, such as str = "article_id", I cannot index the dataframe via df.str: I get an error by doing so. This makes sense, as str is not an attribute of the DataFrame, yet the value of str is an attribute of the DataFrame. How can I index the DataFrame to get the column corresponding to the value of str? I'm looking for some syntax similar to df.valueof(str).
Are there any solutions to this?
From the DataFrames.jl manual's "Getting started" page:
Columns can be directly (i.e. without copying) accessed via df.col, df."col", df[!, :col] or df[!, "col"]. The two latter syntaxes are more flexible as they allow passing a variable holding the name of the column, and not only a literal name.
So you can write df[!, str], and that will be equivalent to df.article_id if str == "article_id".
The Indexing section of the manual goes into even more detail, for when you need more advanced types of indexing or want a deeper understanding of the options.
For an additional reference. When you write:
df.colname
it is equivalent to writing getproperty(df, :colname). Therefore if you have column name stored in the str variable you can write getproperty(df, str).
However, as Sundar R noted it is usually more convenient to use indexing instead of property access. Two most common patterns are df[!, str] which is equivalent to getproperty(df, str) and gets you a column without copying it and df[:, str] which gets you a copy of a column.
I've got a table column (columnName) that holds an object. One of those object's properties (objectKey) is an array of strings. I want to prepend to that array a single value (newValue). I cannot for the life of me figure out how to do this from the DB.
The closest I can get it do the following:
ARRAY['newValue'] || (columnName::jsonb->>'objectKey')
But that escapes all the characters in the original array and is technically making the original array an element in the a new array.
Is there a simple way to do this in the database?
A sample of my column looks like this:
{"objectKey":["one","two","three"],"objectKey2":["one","two","three"]}
After running a script I'd like it to be:
{"objectKey":["newValue","one","two","three"],"objectKey2":["one","two","three"]}
You can use jsonb_set() to add a value to an array. The syntax is a bit clumsy though. You need to "fake" it with a non-existing index.
jsonb_set(the_column, '{objectKey,-9999}'::text[], '"newValue"'::jsonb, true)
The 9999 is simply a non-existing array index and by using a negative value, the new element is added at the beginning of the array as documented in the manual
Alternatively you can use || to concatenate two objects:
To append a value you can use the || operator as suggested by Abelisto:
jsonb_set(the_column, '{objectKey}', (the_column -> 'objectKey') || '["newValue"]')
To add the element at the start:
jsonb_set(the_column, '{objectKey}', '["newValue"]' || (the_column -> 'objectKey'))
Online example
I am trying to pass an array as query parameter in Postman.
I am calling DELETE method to delete a user from list of databases. It is expecting list of database names as array in the query parameter. When I pass as given below, I am getting error.
{"code":2,"message":"data_services must be an array and not empty"}
Please let me know, how can I pass an array as query parameter in Postman.
You need to create 2 keys with the same name and add postfix []
You can specify the parameter key multiple times with different values.
Don't use square brackets or numbers like an array in code.
This will result in a query string like so:
?data_services=somename&data_services=anothername
I mis-read the DELETE method. It was expecting array of databases in the body of the Http body section, instead of on query parameter section. I provided the array as given below and it got succeeded.
The below is the way, I passed array to Http request in the body section.
can you check below link it is useful for you
https://medium.com/#darilldrems/how-to-send-arrays-with-get-or-post-request-in-postman-f87ca70b154e
I'm aware of the option to output a query formatted as JSON, like the following example from the from MSDN page:
SELECT name, surname
FROM emp
FOR JSON AUTO
There are a lot of samples on how to use the resulting json from apps but my question is, how can I store the resulting json in a varchar variable, let's say to store in another table?
DECLARE #Json nvarchar(MAX) = (
SELECT name, surname
FROM emp
FOR JSON AUTO
);
Dan Guzman replied in the MSDN Forum with this neat solution, which corresponds also to #FDavidov's suggestion in his last comment
A JSON is, in fact, a character string. What makes this character string to be a JSON is the combination of two things:
You refer to it as a JSON (using the correct functions within your environment),
It contains the correct delimiters at the correct locations to comply with the rules of a JSON.
So, if you want to store a JSON in a variable, just assign to the variable the source string and, whenever you want to act on your variable, just remember it is a JSON.