I have the following JSON
{
"eventSummaryList": [
{
"customer": "189256",
"data": "{\"cliente\":\"189256\",\"data_posicao\":\"1491426372\",\"gps_valido\":\"1\",\"horimetro\":\"120561\",\"ibuttonHex\":\"0\",\"ibuttonPart1\":\"0\",\"ibuttonPart2\":\"0\",\"id_evento\":\"null\",\"id_motorista\":\"0\",\"ignicao\":\"1\",\"latitude\":\"-2222222\",\"longitude\":\"-2222222\",\"odometro\":\"253692\",\"pos_memoria\":\"0\",\"veiculo\":\"44444\",\"velocidade\":\"50\"}",
"identifierEventRule": "77404",
"identifierRule": "6",
"identifierSummary": "28901976",
"rule": "velocidade_maior_que",
"status": 1,
"vehicle": "44444"
}
],
"header": {
"mensagem": {
"estilo": "SUCCESS",
"mensagem": "Successfully executed service",
"plataforma": "EVENT_POINT",
"status": "SUCESSO"
}
}
}
And I need to extract the value "velocidade" what's inside "data" that
contains another json.
I'm using the following syntax but it returns null.
select cast((value::json ->'data')::json->> 'velocidade' AS int) AS velocidade,
Try:
SELECT ((value::json #>> '{eventSummaryList,0,data}')::json ->> 'velocidade')::int
(The #>> or ->> operators are the key. If you use #> or ->, you'll end up with a json-encoded json string. BTW this is really a messed up model, I would look into fixing its input first/instead.)
http://rextester.com/THVYFK9026
Related
I'm using SQL Server 2016 to create a JSON file with multiple nested elements. The final JSON needs to have two nested arrays on the same level, as in array1a and array1b in the simplified example below, with array1 itself being a nested array in the final document.
{"array1": [
{
"array1a": [
{
"string": "text",
"number": 25.7
}
],
"array1b": [
{
"object": {
"type": "foo",
"value": "bar"
}
}
]
}
]
}
I tried using subqueries with FOR JSON PATH, but which formats array1a and array1b correctly, but array1 always comes back as an object, not as an array.
[array1.array1a] = (SELECT
StringField AS string
, NumberField AS number
FROM table FOR JSON PATH, INCLUDE_NULL_VALUES)
, [array1.array1b] = (SELECT
ObjectType AS [object.type]
, ObjectValue AS [object.value]
FROM table FOR JSON PATH, INCLUDE_NULL_VALUES)
That gives me the JSON below, without the square brackets that wrap the contents of array1.
{"array1": {
"array1a": [
{
"string": "text",
"number": 25.7
}
],
"array1b": [
{
"object": {
"type": "foo",
"value": "bar"
}
}
]
}
}
I also tried concatenating the two sub-queries together as in the example below, but the JSON output is escaped.
[array1] = CONCAT(
(SELECT
StringField AS [array1a.string]
, NumberField AS [array1a.number]
FROM table FOR JSON PATH, INCLUDE_NULL_VALUES)
, (SELECT
ObjectType AS [array1b.object.type]
, ObjectValue AS [array1b.object.value]
FROM table FOR JSON PATH, INCLUDE_NULL_VALUES)
)
Result:
"array1": "[{\"array1a.string\":\"text\",\"array1a.number\":25.7}][{\"array1b\":{\"object\":{\"type\":\"foo\",\"value\":\"bar\"}}}]"
Any thoughts on how I can get two nested arrays at the same hierarchy level that are themselves part of a nested array?
{
"responseCode": "200",
"data": {
"sequence": 1,
"used": true,
"sensingTags": [
{
"code": "LED",
"value": 1,
"updatedOn": 1587557350251
}
]
}
}
My goal is get updatedOn value from this json using jsonPath like this
1587557350251
i thought below jsonPath will work but it extract only empty list.
$..sensingTags[?(#.code == 'LED')][0].updatedOn
And i want to know how to extract value like below
{
"code": "LED",
"value": 1,
"updatedOn": 1587557350251
}
Not like this one.
[
{
"code" : "LED",
"value" : 1,
"updatedOn" : 1587557350251
}
]
As per Getting a single value from a JSON object using JSONPath, JsonPath will always return an array (or a false) at that point...
Best you can do is process it as an array of updatedOn and simply always grab the first value.
$..sensingTags[?(#.code == 'LED')].updatedOn
I'm parsing a JSON document like
{
"status": "ok",
"tick": {
"id": 101006380508,
"data": [
{
"id": "10100638050832281590023",
"amount": 0.2251,
}
]
}
}
I am currently doing:
data = JSON.parse(response.read_body)
data["tick"]["data"]["amount"]
and getting an error
Error: []': no implicit conversion of String into Integer (TypeError)
My objective is the get the amount
It's array that contains one hash, so you can get it as
data["tick"]["data"].first["amount"] # => 0.2251
I am trying to build a JSON from the following table
name | flag
------+------
foo | fail
bar | pass
using the query,
DECLARE #JSONDATA nvarchar(MAX) = (SELECT [name], [flag]
FROM test
FOR JSON AUTO, ROOT('students'))
SET #JSONDATA = JSON_MODIFY(#JSONDATA, '$.class','10')
The generated JSON here is
{
"students": [
{
"name": "foo",
"flag": "fail"
},
{
"name": "bar",
"flag": "pass"
}
],
"class": "10"
}
I need to the class element at the very first node of the JSON. Is there any way, using JSON_MODIFY ?
Fiddle
At a loss forcing a sequence via modify.
Perhaps an alternative
Select class=10
,students = (SELECT [name], [flag] FROM test FOR JSON AUTO)
For JSON path, without_array_wrapper
Returns
{
"class": 10,
"students": [{
"name": "foo",
"flag": "fail"
}, {
"name": "bar",
"flag": "pass"
}]
}
EDIT- Updated SELECT as suggested by GSerg
I have some JSON along the following lines, the format of which cannot, unfortunately, be changed:
{
"elements": {
"nodes": [
{
"data": {
"name": "Name here",
"color": "#FFFFFF",
"id": "n0"
}
}
]
}
}
This is stored in a postgres database and I'd like to pull out records by means of the id embedded in the JSON above. So far I've tried stuff like this:
SELECT "data".* FROM "data" WHERE payload #>> '{elements,nodes,data,id}' = 'n0';
...without success; although this query runs it returns nothing. Can anyone suggest how this might be done?
Create schema:
create table json (
id serial primary key,
data jsonb
);
insert into json (data) values (
'{ "elements": {
"nodes": [
{
"data": {
"name": "Name here",
"color": "#FFFFFF",
"id": "n0"
}
}
]
}
}
'::jsonb);
Query itself:
select * from json js,jsonb_array_elements(data->'elements'->'nodes') as node
where node->'data'->>'id' = 'n0';