<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic How to extract JSON data based on a specific value, and why Splunk reordering my JSON data generated from a script at index-time? in Getting Data In</title>
    <link>https://community.splunk.com/t5/Getting-Data-In/How-to-extract-JSON-data-based-on-a-specific-value-and-why/m-p/281872#M53890</link>
    <description>&lt;P&gt;Hello everybody,&lt;/P&gt;

&lt;P&gt;I have JSON data that I generate from a Python script.&lt;BR /&gt;
It looks like this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;{
    "leaderboard": [
        {
            "action": {
                "name": "total_sparxea_newdiagram",
                "ranking": [
                    {
                        "id": "pierre@dupont.fr",
                        "name": "Pierre dupont",
                        "points": 7,
                        "position": 1
                    },
                    {
                        "id": "NouvelUtilisateur2",
                        "name": "C'est son nom!",
                        "points": 3,
                        "position": 2
                    },
                ]
            }
        },
        {
            "action": {
                "name": "total_click",
                "ranking": [
                    {
                        "id": "allo",
                        "name": "Mr Allo",
                        "points": 3,
                        "position": 1
                    },
                    {
                        "id": "pierre@dupont.fr",
                        "name": "pierre@dupont.fr",
                        "points": 0,
                        "position": 2
                    },
                ]
            }
        },
    ],
    "timestamp": "2016-04-12 14:41:40.173000"
}
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;So I use it to make a kind of ranking of users per actions on a website.&lt;/P&gt;

&lt;P&gt;&lt;STRONG&gt;My first question is:&lt;/STRONG&gt;&lt;BR /&gt;
How to extract some ranking information for a specific action? For example, I need to extract all ranking data for &lt;CODE&gt;action.name = total_click&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;I tried to use spath, and used index in my spath to move to specific action. Here is my search :&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;source=source | spath output=action path=leaderboard{1}.action{}.name | spath output=player path=leaderboard{1}.action{}.ranking{}.id| spath output=position path=leaderboard{1}.action{}.ranking{}.position| spath output=points path=leaderboard{1}.action{}.ranking{}.points |replace "total_gsites_comment" with "Meilleur commentateur gsite" in action | table action,player,position,points
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;So you can see, I used index 1 to get data ranking from action 1. But my problem is that Splunk never indexes my JSON object in the same order, so I can't use indexes. Sometimes &lt;CODE&gt;spath output=action path=leaderboard{1}.action{}.name&lt;/CODE&gt; will be &lt;CODE&gt;total_sparxea_newdiagram&lt;/CODE&gt; and sometimes not. I checked my script output and my JSON is always the same.&lt;/P&gt;

&lt;P&gt;&lt;STRONG&gt;So second question:&lt;/STRONG&gt;&lt;BR /&gt;
Why is Splunk reordering my json object at index-time?&lt;/P&gt;

&lt;P&gt;I really need help and I don't understand. Thanks&lt;/P&gt;</description>
    <pubDate>Tue, 12 Apr 2016 12:58:03 GMT</pubDate>
    <dc:creator>Arismore</dc:creator>
    <dc:date>2016-04-12T12:58:03Z</dc:date>
    <item>
      <title>How to extract JSON data based on a specific value, and why Splunk reordering my JSON data generated from a script at index-time?</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/How-to-extract-JSON-data-based-on-a-specific-value-and-why/m-p/281872#M53890</link>
      <description>&lt;P&gt;Hello everybody,&lt;/P&gt;

&lt;P&gt;I have JSON data that I generate from a Python script.&lt;BR /&gt;
It looks like this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;{
    "leaderboard": [
        {
            "action": {
                "name": "total_sparxea_newdiagram",
                "ranking": [
                    {
                        "id": "pierre@dupont.fr",
                        "name": "Pierre dupont",
                        "points": 7,
                        "position": 1
                    },
                    {
                        "id": "NouvelUtilisateur2",
                        "name": "C'est son nom!",
                        "points": 3,
                        "position": 2
                    },
                ]
            }
        },
        {
            "action": {
                "name": "total_click",
                "ranking": [
                    {
                        "id": "allo",
                        "name": "Mr Allo",
                        "points": 3,
                        "position": 1
                    },
                    {
                        "id": "pierre@dupont.fr",
                        "name": "pierre@dupont.fr",
                        "points": 0,
                        "position": 2
                    },
                ]
            }
        },
    ],
    "timestamp": "2016-04-12 14:41:40.173000"
}
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;So I use it to make a kind of ranking of users per actions on a website.&lt;/P&gt;

&lt;P&gt;&lt;STRONG&gt;My first question is:&lt;/STRONG&gt;&lt;BR /&gt;
How to extract some ranking information for a specific action? For example, I need to extract all ranking data for &lt;CODE&gt;action.name = total_click&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;I tried to use spath, and used index in my spath to move to specific action. Here is my search :&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;source=source | spath output=action path=leaderboard{1}.action{}.name | spath output=player path=leaderboard{1}.action{}.ranking{}.id| spath output=position path=leaderboard{1}.action{}.ranking{}.position| spath output=points path=leaderboard{1}.action{}.ranking{}.points |replace "total_gsites_comment" with "Meilleur commentateur gsite" in action | table action,player,position,points
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;So you can see, I used index 1 to get data ranking from action 1. But my problem is that Splunk never indexes my JSON object in the same order, so I can't use indexes. Sometimes &lt;CODE&gt;spath output=action path=leaderboard{1}.action{}.name&lt;/CODE&gt; will be &lt;CODE&gt;total_sparxea_newdiagram&lt;/CODE&gt; and sometimes not. I checked my script output and my JSON is always the same.&lt;/P&gt;

&lt;P&gt;&lt;STRONG&gt;So second question:&lt;/STRONG&gt;&lt;BR /&gt;
Why is Splunk reordering my json object at index-time?&lt;/P&gt;

&lt;P&gt;I really need help and I don't understand. Thanks&lt;/P&gt;</description>
      <pubDate>Tue, 12 Apr 2016 12:58:03 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/How-to-extract-JSON-data-based-on-a-specific-value-and-why/m-p/281872#M53890</guid>
      <dc:creator>Arismore</dc:creator>
      <dc:date>2016-04-12T12:58:03Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract JSON data based on a specific value, and why Splunk reordering my JSON data generated from a script at index-time?</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/How-to-extract-JSON-data-based-on-a-specific-value-and-why/m-p/281873#M53891</link>
      <description>&lt;P&gt;I can't answer why Splunk is reordering your JSON, but I think I can help with your first question and hopefully solve your problem.&lt;BR /&gt;
The following code should give you all the stats you need for action.name = "total_click":&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;source=source 
| spath path=leaderboard{}
| mvexpand leaderboard{}
| spath input=leaderboard{}
| search action.name = "total_click"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;EDIT to include final query by @Arismore:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;source = "D:\\Logiciels\\Splunk\\etc\\apps\\gamification\\bin\\CupRanking.py" 
 | head 1 
 |spath path=leaderboard{} 
 | mvexpand leaderboard{} 
 | spath input=leaderboard{} 
 | search action.name = "total_gsites_comment" 
 | rename action.name as action_name,action.ranking{}.position as position,action.ranking{}.name as name,action.ranking{}.points as points 
 | table position,name,points
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 12 Apr 2016 17:49:51 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/How-to-extract-JSON-data-based-on-a-specific-value-and-why/m-p/281873#M53891</guid>
      <dc:creator>javiergn</dc:creator>
      <dc:date>2016-04-12T17:49:51Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract JSON data based on a specific value, and why Splunk reordering my JSON data generated from a script at index-time?</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/How-to-extract-JSON-data-based-on-a-specific-value-and-why/m-p/281874#M53892</link>
      <description>&lt;P&gt;Hello, thanks for your answer.&lt;/P&gt;

&lt;P&gt;I tried your solution Javiergn, but it doesn't work.&lt;/P&gt;

&lt;P&gt;I read the doc, mvexpand is a good idea. So i tried  &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;source=source 
 | spath path=leaderboard{}
 | mvexpand leaderboard{}
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;But the mvexpand return me 2 events ( for all my actions ), but my event it's still complete, it just duplicate my first event.&lt;/P&gt;</description>
      <pubDate>Wed, 13 Apr 2016 08:09:51 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/How-to-extract-JSON-data-based-on-a-specific-value-and-why/m-p/281874#M53892</guid>
      <dc:creator>Arismore</dc:creator>
      <dc:date>2016-04-13T08:09:51Z</dc:date>
    </item>
    <item>
      <title>Re: How to extract JSON data based on a specific value, and why Splunk reordering my JSON data generated from a script at index-time?</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/How-to-extract-JSON-data-based-on-a-specific-value-and-why/m-p/281875#M53893</link>
      <description>&lt;P&gt;Nvm, thanks Javiergn it works ! I accept your answer because it's more general, but here is  my full query.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;source = "D:\\Logiciels\\Splunk\\etc\\apps\\gamification\\bin\\CupRanking.py" 
| head 1 
|spath path=leaderboard{} 
| mvexpand leaderboard{} 
| spath input=leaderboard{} 
| search action.name = "total_gsites_comment" 
| rename action.name as action_name,action.ranking{}.position as position,action.ranking{}.name as name,action.ranking{}.points as points 
| table position,name,points
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 13 Apr 2016 09:29:42 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/How-to-extract-JSON-data-based-on-a-specific-value-and-why/m-p/281875#M53893</guid>
      <dc:creator>Arismore</dc:creator>
      <dc:date>2016-04-13T09:29:42Z</dc:date>
    </item>
  </channel>
</rss>

