4
$\begingroup$

This is JSON I'm trying to work within Mathematica:

{
  "status": "success",
  "data": {
    "resultType": "matrix",
    "result": [
      {
        "metric": {
          "name": "scrapes"
        },
        "values": [
          [
            1556901190.699,
            "91158"
          ],
          [
            1556901220.699,
            "91160"
          ]
        ]
      }
    ]
  }
}

What I need to do, is to extract values part of the JSON mentioned above and put them in the list like below for further processing:

Out[107]= {{1556901190.699, 91158}, {1556901220.699, 91160}}

I'd appreciate assistance with the implementation details. Thanks in advance.

$\endgroup$

1 Answer 1

6
$\begingroup$

Use the RawJSON Import format.

In[22]:= json = ImportString["{
    \"status\": \"success\",
    \"data\": {
      \"resultType\": \"matrix\",
      \"result\": [
        {
          \"metric\": {
            \"name\": \"scrapes\"
          },
          \"values\": [
            [
              1556901190.699,
              \"91158\"
            ],
            [
              1556901220.699,
              \"91160\"
            ]
          ]
        }
      ]
    }
  }", "RawJSON"]

Out[22]= <|"status" -> "success", 
 "data" -> <|"resultType" -> "matrix", 
   "result" -> {<|"metric" -> <|"name" -> "scrapes"|>, 
      "values" -> {{1.5569*10^9, "91158"}, {1.5569*10^9, 
         "91160"}}|>}|>|>

In[23]:= json[["data", "result", 1, "values"]]    
Out[23]= {{1.5569*10^9, "91158"}, {1.5569*10^9, "91160"}}
$\endgroup$
1
  • 1
    $\begingroup$ Thank you. It works as expected. $\endgroup$ May 3, 2019 at 17:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.