# How to access json objects

**URL:** <https://discuss.elastic.co/t/how-to-access-json-objects/143703>\
**Category:** Logstash\
**Created:** [August 9, 2018, 1:10pm UTC](https://discuss.elastic.co/t/how-to-access-json-objects/143703 "2018-08-09T13:10:37Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gegenki](https://avatars.discourse-cdn.com/v4/letter/g/e36b37/32.png) [@Gegenki](https://discuss.elastic.co/u/Gegenki)\
**Post date:** [August 9, 2018, 1:10pm UTC](https://discuss.elastic.co/t/how-to-access-json-objects/143703/1 "2018-08-09T13:10:37Z")

</div>

I cannot figure out how to access an array or flatten it.

I get input from an SQL database. I use jdbc\_streaming to get more data

```
jdbc_streaming {
  jdbc_connection_string => "jdbc:postgresql://localhost:5432/db"
  statement => "SELECT user_id FROM posts WHERE post_id = :post_id"
  parameters => {"post_id" => "post_id"}
  target => "user_id"
}

```

This creates this absolutely ridiculous object that keys against what is returned by my database  
So the return object looks like this

```
{
  "user_id": "00000000-0000-0000-0000-000000000000"
}

```

But that is ok. The problem is I now want to get the username with another lookup

```
jdbc_streaming {
  jdbc_connection_string => "jdbc:postgresql://localhost:5432/db"
  statement => "SELECT username FROM users WHERE user_id = :user_id"
  parameters => {"user_id" => "user_id"}
  target => "username"
}

```

Obviously this doesn't work because it sends  
SELECT username FROM users WHERE user\_id = (user\_id = '00000000-0000-0000-0000-000000000000')

So then how do I access my user id?  
parameters =\> {"user\_id" =\> "user\_id"}  
parameters =\> {"user\_id" =\> "%{[user\_id]}"}  
parameters =\> {"user\_id" =\> "%{[user\_id][0][user\_id]}"}  
parameters =\> {"user\_id" =\> "%{[user\_id][user\_id]}"}

None of these work.  
How am I supposed to do this  
OR - how do I normalise the user\_id field so that instead of looking like this

```
{
  "user_id": "00000000-0000-0000-0000-000000000000"
}

```

it is just  
`00000000-0000-0000-0000-000000000000`

---

<div class="post-metadata">

**Author:** ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)\
**Post date:** [August 9, 2018, 1:25pm UTC](https://discuss.elastic.co/t/how-to-access-json-objects/143703/2 "2018-08-09T13:25:30Z")

</div>

> So the return object looks like this
> 
> ```auto
> {
> post_id: 00000000-0000-0000-0000-000000000000
> }
> 
> ```

That's not valid JSON, so just to be clear, do you mean

```plaintext
{
  "post_id": "00000000-0000-0000-0000-000000000000"
}

```

? Or, given what you write later in your post,

```plaintext
{
  "user_id": "00000000-0000-0000-0000-000000000000"
}

```

?

> So then how do I access my user id?  
> parameters =\> {"user\_id" =\> "user\_id"}  
> parameters =\> {"user\_id" =\> "%{[user\_id]}"}  
> parameters =\> {"user\_id" =\> "%{[user\_id][0][user\_id]}"}  
> parameters =\> {"user\_id" =\> "%{[user\_id][user\_id]}"}

Probably with `[user_id][post_id]` or `[user_id][user_id]` depending on what exactly the field contains.

---

<div class="post-metadata">

**Author:** ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)\
**Post date:** [August 9, 2018, 1:27pm UTC](https://discuss.elastic.co/t/how-to-access-json-objects/143703/3 "2018-08-09T13:27:36Z")

</div>

It would be easier to answer this if you showed us what you get from

```
output { stdout { codec => rubydebug } }
```

---

<div class="post-metadata">

**Author:** ![Gegenki](https://avatars.discourse-cdn.com/v4/letter/g/e36b37/32.png) [@Gegenki](https://discuss.elastic.co/u/Gegenki)\
**Post date:** [August 9, 2018, 1:30pm UTC](https://discuss.elastic.co/t/how-to-access-json-objects/143703/4 "2018-08-09T13:30:59Z")

</div>

Sorry, made some silly mistakes, switching post\_id and user\_id - correct now

---

<div class="post-metadata">

**Author:** ![Gegenki](https://avatars.discourse-cdn.com/v4/letter/g/e36b37/32.png) [@Gegenki](https://discuss.elastic.co/u/Gegenki)\
**Post date:** [August 9, 2018, 1:40pm UTC](https://discuss.elastic.co/t/how-to-access-json-objects/143703/6 "2018-08-09T13:40:59Z")

</div>

parameters =\> {"user\_id" =\> "[user\_id][user\_id]"}

Does not work either.  
Always the query at my database is  
`SELECT username FROM users WHERE user_id = NULL`

---

<div class="post-metadata">

**Author:** ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)\
**Post date:** [August 9, 2018, 1:47pm UTC](https://discuss.elastic.co/t/how-to-access-json-objects/143703/7 "2018-08-09T13:47:10Z")

</div>

Please show the result of an `stdout { codec => rubydebug }` output.

---

<div class="post-metadata">

**Author:** ![Gegenki](https://avatars.discourse-cdn.com/v4/letter/g/e36b37/32.png) [@Gegenki](https://discuss.elastic.co/u/Gegenki)\
**Post date:** [August 9, 2018, 1:48pm UTC](https://discuss.elastic.co/t/how-to-access-json-objects/143703/8 "2018-08-09T13:48:32Z")

</div>

It looks like this

```
{
  "tags" => [
    [0] "_jdbcstreamingdefaultsused"
  ],
  "@timestamp" => 2018-08-09T13:45:06.449Z,
  "type" => "post_data",
   "created" => 2018-01-27T12:40:55.405Z,
   "@version" => "1",
   "user_id" => [
     [0] {
       "user_id" => "c99e074b-f328-48b5-89f8-4993ae1c4d3d"
     }
   ],
   "username" => [
     [0] {}
  ]
}
```

---

<div class="post-metadata">

**Author:** ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)\
**Post date:** [August 9, 2018, 1:51pm UTC](https://discuss.elastic.co/t/how-to-access-json-objects/143703/9 "2018-08-09T13:51:12Z")

</div>

Okay. Then I'd expect `[user_id][0][user_id]` to work. If it doesn't I don't know what the problem is.

Wouldn't another option be to adjust the SQL query to flatten the contents of the column?

---

<div class="post-metadata">

**Author:** ![Gegenki](https://avatars.discourse-cdn.com/v4/letter/g/e36b37/32.png) [@Gegenki](https://discuss.elastic.co/u/Gegenki)\
**Post date:** [August 9, 2018, 1:53pm UTC](https://discuss.elastic.co/t/how-to-access-json-objects/143703/10 "2018-08-09T13:53:32Z")

</div>

The SQL query is flat  
It returns a single column with a single value

- I don't know how to get logstash to store only the value

^ But that did the trick though -\> `[user_id][0][user_id]` works  
It isn't clear to me when `%{[user_id][0][user_id] }` should be used

- Thanks!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [September 6, 2018, 2:03pm UTC](https://discuss.elastic.co/t/how-to-access-json-objects/143703/11 "2018-09-06T14:03:25Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
