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$
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
    $\begingroup$ Thank you. It works as expected. $\endgroup$ – Pavel Klymenko May 3 at 17:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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