Why is json.Marshal seemingly producing an array of ints? - json

I'm trying to wrap my head around the Go language and I've hit my first stumbling block with this simple example:
package main
import (
"encoding/json"
"fmt"
)
type MyStructure struct {
Integer int `json:"integer"`
Label string `json:"label"`
}
func main() {
ms := &MyStructure{9001, "over9000"}
msjson, _ := json.Marshal(ms)
fmt.Println(msjson) // expect: {"integer": 9001, "label": "over9000"}
}
My output is as follows: [123 34 105 110 116 101 103 101 114 34 58 57 48 48 49 44 34 108 97 98 101 108 34 58 34 111 118 101 114 57 48 48 48 34 125]
I'm clearly missing something obvious; could someone please point me in the right direction?

It produces a byte slice (ref : http://golang.org/pkg/encoding/json/#Marshal), use string(msjson) to get the string.
Also never ignore errors, it bites you back whenever you least expect it.
fmt.Println(string(msjson))
// or
fmt.Printf("%s\n", msjson) //shamelessly taken from #dustin's comment

Related

is possible to create a map with 2 keys and a vector of values in Clojure?

I am trying to create a program that reads in a table of temperatures from a csv file and would like to access a a collection of temperatures based on the year and day.
the first column stands for the year the tempratures have been recorded.
the second column stands for a specific day during each month .
the rest of the column represent the temperatures each month.
For example, 2021 - 23 - 119 = 23rd June 2021 has a temperature of 119
Year Day Months from January to December
2018 18 | 45 54 -11 170 99 166 173 177 175 93 74 69
2021 23 | 13 87 75 85 85 119 190 172 156 104 39 53
2020 23 | 63 86 62 128 131 187 163 162 138 104 60 70
So far I have managed to load the data from a CSV File with clojure.data.csv. this returns a sequence of vectors into the program
(defn Load_csv_file [filepath]
(try
(with-open [reader (io/reader filepath)]
(.skip reader 1)
( let [data (csv/read-csv reader)]
(println data) )
)
(catch Exception ex (println (str "LOL Exception: " (.toString ex))))
))
I am currently trying to figure out how to implement this but my reasoning was to create three keys in a map which will take in the year, day and vector of temperatures, to then filter for a specific value.
Any advice on how i can implement this functionality.
Thanks!
i would go with something like this:
(require '[clojure.java.io :refer [reader]]
'[clojure.string :refer [split blank?]]
'[clojure.edn :as edn])
(with-open [r (reader "data.txt")]
(doall (for [ln (rest (line-seq r))
:when (not (blank? ln))
:let [[y d & ms] (mapv edn/read-string (split ln #"\s+\|?\s+"))]]
{:year y :day d :months (vec ms)})))
;;({:year 2018,
;; :day 18,
;; :months [45 54 -11 170 99 166 173 177 175 93 74 69]}
;; {:year 2021,
;; :day 23,
;; :months [13 87 75 85 85 119 190 172 156 104 39 53]}
;; {:year 2020,
;; :day 23,
;; :months [63 86 62 128 131 187 163 162 138 104 60 70]})
by the way, i'm not sure csv format allows different separators (as you have in your example.. anyway this one would work for that)
I would create a map of data that looked something like this
{2020 {23 {:months [63 86 62 128 131 187 163 162 138 104 60 70]}}}
This way you can get the data out in a fairly easy way
(get-in data [2020 23 :months]
So something like this
(->> (Load_csv_file "file.csv")
(reduce (fn [acc [year day & months]] (assoc-in acc [year day] months)) {}))
This will result in the data structure I mentioned now you just need to figure out the location of the data you want

Go: Reading JSON file vs. Reading JSON string

I have a JSON file called example.json that looks like:
{
"name": "example",
"type": "record"
}
I also have a variable representing the above as a "string":
const example = `{
"name": "example",
"type": "record",
}`
I am trying to understand why reading the contents of the JSON file into bytes is different from reading the contents of the example variable. My code is as follows:
bytesJSON, err := ioutil.ReadFile("example.json")
if err != nil {
fmt.Println(err)
}
bytesVar, err := json.Marshal(example)
if err != nil {
fmt.Println(err)
}
Both are of the type []uint8, but look very different. Any ideas on why? And how I can make sure that they are always the same?
EDIT: Even using bytesVar := []byte(example) results in the same issue.
EDIT:
bytesJSON looks like:
[123 10 32 32 32 32 34 110 97 109 101 34 58 32 34 101 120 97 109 112 108 101 34 44 10 32 32 32 32 34 116 121 112 101 34 58 32 34 114 101 99 111 114 100 34 10 125]
bytesVar looks like:
[34 112 117 98 115 117 98 95 101 120 97 109 112 108 101 95 116 111 112 105 99 34]
when printed to stdout.
Note: The "edit" output in the question is using different example input than in the question.
If we print them as strings it becomes clear.
fmt.Println(string(bytesJSON))
{
"name": "example",
"type": "record",
}
ioutil.ReadFile is just what's in the file.
fmt.Println(string(bytesVar))
"{\n \"name\": \"example\",\n \"type\": \"record\",\n }"
json.Marshal has encoded the string example as JSON. That is a JSON string containing a string.
The equivalent to ioutil.ReadFile("example.json") is simply example.
If we unmarshal bytesVar we get back the original string in example.
var unmarshal string;
json.Unmarshal(bytesVar,&unmarshal)
fmt.Println(unmarshal)
{
"name": "example",
"type": "record",
}

Importing Textfile as Matrix

i'm trying to import a txt-File to octave which contains a matrix of data.
The matrix looks like this:
49 ..1. ...1.......... ..... 49
47 ..12 ...1...... ... ..... 47
45 ..12....1...... 2....1... 45
43 ....2....1...... 2...1.... 43
41 .1..2.. .........2. .1..... 41
39 .1.12.2....1.....2. .1..... 39
37 .1..2.22...1.....2. .1..... 37
35 .1. 2222...2....2....1.1... 35
33 ....22.2...2....2....12.... 33
31 ....22.2...2..........21... 31
29 .....2.2...2.....2....21... 29
27 ........222222....2....21.... 27
25 .......22.2222....2.22.2..... 25
23 .......22.2222....2.2..2..... 23
21 .......222.222....2.2........ 21
19 ........22.222....2.......... 19
17 ..........2.2.2...22........... 17
15 ............................... 15
13 .......................2....... 13
11 .......................2......2 11
9 ........................2.....222 9
7 . ................. ..... ......222. 7
5 .................. ....1.. 5
3 ....... ......... .... 3
1 1
This is actually map/coordinate system (y-axis=azimuth, x-axis=latitude), which i have to plot. (blank=no data, .=no effects, 1=weak effects, 2=strong eff.)
The result should look like this
Because i failed to import this txt-File, i changed it into this.
49;1;1;1;1;1;1;1;2;2;3;2;1;2;2;2;3;2;2;2;2;2;2;2;2;2;2;1;2;2;2;2;2;1;1;1;1;1;1;1;49
47;1;1;1;1;1;1;1;2;2;3;4;1;2;2;2;3;2;2;2;2;2;2;1;2;2;2;1;2;2;2;2;2;1;1;1;1;1;1;1;47
45;1;1;1;1;1;1;1;2;2;3;4;2;2;2;2;3;2;2;2;2;2;2;1;4;2;2;2;2;3;2;2;2;1;1;1;1;1;1;1;45
43;1;1;1;1;1;1;2;2;2;2;4;2;2;2;2;3;2;2;2;2;2;2;1;4;2;2;2;3;2;2;2;2;1;1;1;1;1;1;1;43
39;1;1;1;1;1;1;2;3;2;3;4;2;4;2;2;2;2;3;2;2;2;2;2;4;2;1;2;3;2;2;2;2;2;1;1;1;1;1;1;39
37;1;1;1;1;1;1;2;3;2;2;4;2;4;4;2;2;2;3;2;2;2;2;2;4;2;1;2;3;2;2;2;2;2;1;1;1;1;1;1;37
and so on.
This is working with my code.
RawMap = dlmread('C:\Desktop\2576.map', ';', 0:80, 0:24)
Map = flipud(RawMap)
pcolor(Map(:,2:end-1))
To this automatically i don't want to the change the code. So i need to get the original file imported.
Any suggestions?
Thanks
Here is one approach to parse the file meaningfully:
S = fileread('testo.txt');
S = strsplit (S, "\n");
S = strvcat( S );
S = double(S);
S = S(:, 4:end-4);
S( S == double(" ") ) = 0;
S( S == double(".") ) = 1;
S( S == double("1") ) = 2;
S( S == double("2") ) = 3;
pcolor(S); axis ij;

Why do I convert map to json, map includes list values which is nothing after converting to json

func Test_JsonTtransfer(t *testing.T) {
uid := "306"
phoneList := list.New()
phoneList.PushBack("18513622928")
fmt.Println("phoneList=======", phoneList.Len())
jsonPhoneList, err := json.Marshal(phoneList)
if err != nil {
fmt.Println("error:", err)
}
fmt.Println("jsonPhoneList=======", string(jsonPhoneList))
idCardList := list.New()
idCardList.PushBack("230405197608040640")
request := make(map[string]interface{})
request["uid"] = uid
request["phones"] = phoneList
request["id_cards"] = idCardList
json, err := json.Marshal(request)
if err != nil {
fmt.Println("error:", err)
}
fmt.Println("json=======", json)
fmt.Println("json=======", string(json))
}
Output:
D:/Sys/server/Go\bin\go.exe test -v golang-test/com/http/test -run ^Test_JsonTtransfer$
phoneList======= 1
jsonPhoneList======= {}
json======= [123 34 105 100 95 99 97 114 100 115 34 58 123 125 44 34 112 104 111 110 101 115 34 58 123 125 44 34 117 105 100 34 58 34 51 48 54 34 125]
json======= {"id_cards":{},"phones":{},"uid":"306"}
ok golang-test/com/http/test 0.482s
Phones should be list values, but nothing. Help me.
Because the List type has no exported fields and the type does not implement the Marshalerinterface, List values always marshal to the text {}.
A simple fix is to use a slice instead of a list:
var phoneList []string
phoneList = append(phoneList, "18513622928")
fmt.Println("phoneList=======", len(phoneList)
playground example

convert plenty of json objects into dataframe R

I have plenty of json objects under the same format in one json file
like below. And I want to convert them into R dataframe and then to extract all the value of lantency.But when I enter the command
json_data <- fromJSON(file=json_flie)
only the first json object is stored in the dataframe, so what should I do???
Thanks!
{"task":[{"type":"ping","id":1,"value":" 159 159 152 153 149 147 150 151 148 149","IsFinished":true},{"type":"latency","id":2,"value":147,"IsFinished":true},{"type":"throughput","id":3,"value":"","IsFinished":false},{"type":"DNS","id":4,"value":12,"IsFinished":true}],"measurementTimes":10,"url":""}{"task":[{"type":"ping","id":1,"value":" 166 165 179 181 159 162 166 159 161 162","IsFinished":true},{"type":"latency","id":2,"value":159,"IsFinished":true},{"type":"throughput","id":3,"value":"","IsFinished":false},{"type":"DNS","id":4,"value":7,"IsFinished":true}],"measurementTimes":10,"url":""}{"task":[{"type":"ping","id":1,"value":" 172 172 159 160 159 159 159 158 160 162","IsFinished":true},{"type":"latency","id":2,"value":158,"IsFinished":true},{"type":"throughput","id":3,"value":"","IsFinished":false},{"type":"DNS","id":4,"value":14,"IsFinished":true}],"measurementTimes":10,"url":""}{"task":[{"type":"ping","id":1,"value":" 182 192 171 184 160 159 156 157 180 171","IsFinished":true},{"type":"latency","id":2,"value":156,"IsFinished":true},{"type":"throughput","id":3,"value":"","IsFinished":false},{"type":"DNS","id":4,"value":26,"IsFinished":true}],"measurementTimes":10,"url":""}{"task":[{"type":"ping","id":1,"value":" 158 186 168 189 190 233 168 160 188 157","IsFinished":true},{"type":"latency","id":2,"value":157,"IsFinished":true},{"type":"throughput","id":3,"value":"","IsFinished":false},{"type":"DNS","id":4,"value":1,"IsFinished":true}],"measurementTimes":10,"url":""}
Your input JSON is malformed, and has multiple elements "task" at the root level. This is akin to defining an XML document with more than one root, which is of course not allowed. If you create an outer element which contains an array of "task" elements, then you will be able to successfully load the file into R using fromJSON. Here is what the file should look like:
{
"root" : [
{
"task":
[
{"type":"ping","id":1,"value":" 159 159 152 153 149 147 150 151 148 149","IsFinished":true},
{"type":"latency","id":2,"value":147,"IsFinished":true},
{"type":"throughput","id":3,"value":"","IsFinished":false},
{"type":"DNS","id":4,"value":12,"IsFinished":true}
],
"measurementTimes":10,
"url":""
},
{
"task":
[
{"type":"ping","id":1,"value":" 166 165 179 181 159 162 166 159 161 162","IsFinished":true},
{"type":"latency","id":2,"value":159,"IsFinished":true},
{"type":"throughput","id":3,"value":"","IsFinished":false},\
{"type":"DNS","id":4,"value":7,"IsFinished":true}
],
"measurementTimes":10,
"url":""
},
... and so on for other entries
]
}
Here is what I saw in the R console:
> summary(json_data)
Length Class Mode
root 5 -none- list
And entering the variable name json_data gave me a dump on the entire JSON structure.