Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to extract values from nested json objects using JSON Extractor #24

Closed
sarath-rachuri opened this issue Feb 1, 2016 · 9 comments
Closed
Assignees

Comments

@sarath-rachuri
Copy link

I have a json file that contains a few properties, say,

{
  "A": [
    {
      "property1": "value 1",
    },
    {
      "A1": [
        {
          "property2": "value 2",
          "property3": "value 3"
        }
      ],
      "property4": "value 4"
    }
  ],
  "B": {
     "custom": {
      "B1": [
        {
           "property5": "value 5",
           "property6": "value 6",
           "property7": "value 7"
        }        
      ],
      "metrics": [ ]
    },
    "property8": { }
  }
}

Few of them are nested. How to extract the nested property values using JSON extractor. For example, I want to extract the values of properties 5,6,7.

@nibras85
Copy link

Hi. Any updates regarding this bug?

@rukmanig
Copy link
Contributor

rukmanig commented Apr 27, 2016

Hi, you can use the JsonTuple function to get the values, the code snippet to extract the properties 5,6 and 7 is done as follows

@jsonnodes =
SELECT JsonApp.JsonFunctions.JsonTuple(jsonstring, "$..property5", "$..property6", "$..property7") AS json_map
FROM @JSON;

@nodes_exploded = SELECT name, node_value
FROM @jsonnodes
CROSS APPLY
EXPLODE(json_map) AS T(name, node_value);

The snippet above will give you an output as follows
"custom.B1[0].property5", "value 5"
"custom.B1[0].property6" , "value 6"
"custom.B1[0].property7", "value 7"

Please let me know if this helps and if you have any further questions.

@chadequate
Copy link

Thanks for the reply! What does @b1_property refer to here?

@rukmanig
Copy link
Contributor

Sorry Chad, updated to fix typo in code to call the right variable.

@chadequate
Copy link

I don't quite understand what jsonstring refers to here. Isn't the input coming from @JSON?

Sorry for the sporadic questioning. I keep getting pulled onto other things :)

@rukmanig
Copy link
Contributor

rukmanig commented Jun 3, 2016

jsonstring is the string you extracted out of the JSON using the JSONExtractor. I have also published code that parses Json on the github site - please do take a look at https://github.com/Azure/usql/blob/master/Examples/JsonSample/JsonSample/NestedJsonParsing.usql that will give the full script that starts with extracting content from a JSON file and manipulating the data. Please let me know if you have any questions.

@chadequate
Copy link

I'm able to extract nested objects now. Thanks for all your help!

@rukmanig
Copy link
Contributor

rukmanig commented Jun 4, 2016

Excellent thanks Chad.

@rukmanig rukmanig closed this as completed Jun 4, 2016
@GauravSagne
Copy link

Hi rukmanig,

I've a nested json for which I've used JsonFunction.JsonTuple() in conjuction with EXPLODE() but that nested json have a sub-properties too. Could you please tell me how to read the sub-properties using JsonFunction.JsonTuple (), please ?

Thanks

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

No branches or pull requests

6 participants