Search code examples
apldyalog

Dyalog APL: convert JSON to a pure array


How to convert JSON to a pure array?

I have JSON like this

{
    "lastUpdateId": 40104114357,
    "bids": [
        [
            "35074.17000000",
            "1.70502000"
        ],
        [
            "35073.93000000",
            "0.00070000"
        ],
        [
            "35073.71000000",
            "0.00066000"
        ]
    ],
    "asks": [
        [
            "35074.18000000",
            "3.85961000"
        ],
        [
            "35074.19000000",
            "0.07203000"
        ],
        [
            "35074.20000000",
            "0.02492000"
        ]
    ]
}

I first set the JSON code to the json variable, and then try the following:

 cmd.URL ← 'api.binance.com/api/v3/depth?symbol=BTCUSDT&limit=3'
      ⊢response←cmd.Run
[rc: 0 | msg:  | HTTP Status: 200 "OK" | ≢Data: 238]
      json ← response.Data
      json
{"lastUpdateId":40104167506,"bids":[["35055.80000000","3.00810000"],["35055.55000000","0.57010000"],["35055.54000000","0.1
      0000000"]],"asks":[["35055.81000000","0.88501000"],["35055.88000000","0.01288000"],["35055.89000000","0.02704000"]]}
      data ← ⎕JSON json
      data.asks
┌───────────────────────────┬───────────────────────────┬───────────────────────────┐
│┌──────────────┬──────────┐│┌──────────────┬──────────┐│┌──────────────┬──────────┐│
││35055.81000000│0.88501000│││35055.88000000│0.01288000│││35055.89000000│0.02704000││
│└──────────────┴──────────┘│└──────────────┴──────────┘│└──────────────┴──────────┘│
└───────────────────────────┴───────────────────────────┴───────────────────────────┘

But data.asks contains NameSpace with sub-namespace, but I need just array, like this:

┌────────────────┬────────────────┬────────────────┐
│35055.81 0.88501│35055.88 0.01288│35055.89 0.02704│
└────────────────┴────────────────┴────────────────┘

How do I do that?

I read its:

but it didn't help me


Solution

  • Since the numbers are represented as valid JSON strings, just use ⎕JSON again:

          ⎕JSON¨¨json.asks
    ┌────────────────┬────────────────┬───────────────┐
    │35074.18 3.85961│35074.19 0.07203│35074.2 0.02492│
    └────────────────┴────────────────┴───────────────┘
          1+⎕JSON¨¨json.asks
    ┌────────────────┬────────────────┬───────────────┐
    │35075.18 4.85961│35075.19 1.07203│35075.2 1.02492│
    └────────────────┴────────────────┴───────────────┘