My input json data is this (cannot be changed, from an external resource):
[{
"Url": "test.url",
"Name": "testname"
},{
"FormName": "Test - 2018",
"FormNumber": 43,
"FormSlug": "test-2018"
}]
I have two structs that will always match the data within the array:
type UrlData struct{
"Url" string `json:Url`
"Name" string `json:Name`
}
type FormData struct{
"FormName" string `json:FormName`
"FormNumber" string `json:FormNumber`
"FormSlug" string `json:FormSlug`
}
Obviously the code below will not work, but is it possible at the top level (or otherwise) to declare something like this:
type ParallelData [
urlData UrlData
formData FormData
]
Use a two step process for unmarshaling. First, unmarshal a list of arbitrary JSON, then unmarshal the first and second element of that list into their respective types.
You can implement that logic in a method called UnmarshalJSON, thus implementing the json.Unmarshaler interface. This will give you the compound type you are looking for:
type ParallelData struct {
UrlData UrlData
FormData FormData
}
// UnmarshalJSON implements json.Unmarshaler.
func (p *ParallelData) UnmarshalJSON(b []byte) error {
var records []json.RawMessage
if err := json.Unmarshal(b, &records); err != nil {
return err
}
if len(records) < 2 {
return errors.New("short JSON array")
}
if err := json.Unmarshal(records[0], &p.UrlData); err != nil {
return err
}
if err := json.Unmarshal(records[1], &p.FormData); err != nil {
return err
}
return nil
}
Try it on the playground: https://play.golang.org/p/QMn_rbJj-P-
I think Answer of Peter is awesome.
Option 1:
type ParallelData [
urlData UrlData
formData FormData
]
if you need above structure then you can define it as
type UrlData struct {
Url string `json:"Url,omitempty"`
Name string `json:"Name,omitempty"`
}
type FormData struct {
FormName string `json:"FormName,omitempty"`
FormNumber string `json:"FormNumber,omitempty"`
FormSlug string `json:"FormSlug,omitempty"`
}
type ParallelData struct {
UrlData UrlData `json:"UrlData,omitempty"`
FormData FormData `json:"FormData,omitempty"`
}
In this case, your json will look like
[
{
"UrlData":{
"Url":"test.url",
"Name":"testname"
}
},
{
"FormData":{
"FormName":"Test - 2018",
"FormNumber":"43",
"FormSlug":"test-2018"
}
}
]
Option 2:
You've provide following json:
[
{
"Url":"test.url",
"Name":"testname"
},
{
"FormName":"Test - 2018",
"FormNumber":43,
"FormSlug":"test-2018"
}
]
If your json really look like, then you can use following struct
type UrlData struct {
Url string `json:Url`
Name string `json:Name`
}
type FormData struct {
FormName string `json:FormName`
FormNumber int `json:FormNumber`
FormSlug string `json:FormSlug`
}
type ParallelData struct {
UrlData
FormData
}
For both options, you can Unmarshall your json like this
var parallelData []ParallelData
err := json.Unmarshal([]byte(str), ¶llelData)
if err != nil {
panic(err)
}
fmt.Println(parallelData)
See option 1 in playground
See option 2 in playground
You can unmarshal into a map[string]interface{} for example:
type ParallelData map[string]interface{}
func main() {
textBytes := []byte(`[
{
"Url": "test.url",
"Name": "testname"
},
{
"FormName": "Test - 2018",
"FormNumber": 43,
"FormSlug": "test-2018"
}]`)
var acc []ParallelData
json.Unmarshal(textBytes, &acc)
fmt.Printf("%+v", acc)
}
Output:
=> [map[Url:test.url Name:testname] map[FormName:Test - 2018 FormNumber:43 FormSlug:test-2018]]
Playground
Related
Essentially I would like to parse the JSON received from a url and display the data inside of alert popup.
I am able to do this with a simple array below but don't know how this can be done with an JSON array from a URL.
Currently working:
let jsonObj:[String: Any] = ["error": [
"email": ["The email has already been taken."],
"phone": ["The phone has already been taken."]]
]
if let errorMsgs = jsonObj["error"] as? [String: [String]] {
let errMsg = errorMsgs.values.map { $0.reduce("", +) }.joined(separator: "\n")
print(errMsg)
self.presentAlert(withTitle: "Try again", message: errMsg)
}
JSON Array:
var structure = [Struct]()
private func fetchJSON() {
guard let url = URL(string: "url.com")
else { return }
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.httpBody = "code=\(codeValue)".data(using: .utf8)
URLSession.shared.dataTask(with: request) { data, _, error in
guard let data = data else { return }
do {
self.structure = try JSONDecoder().decode([Struct].self,from:data)
}
catch {
print(error)
}
}.resume()
}
struct Struct: Decodable {
let id: Int
let reasons: String
}
Sample JSON Array:
[
{
"id": 1,
"reasons": "Test"
},
{
"id": 3,
"reasons": "Test"
}
]
How can I get the reasons string from the array to populate in the alert similar to above.
UPDATE:
To Print the records in the alert I have done the following but it only prints one record in the alert:
for item in self.structure {
print(item.reasons)
let errMsg = [item.reasons].compactMap { $0 }.joined(separator: "\n")
self.presentAlert(withTitle: "Try again", message: errMsg)
}
Here I have created example from your code.
First of all replace your Codable with:
// MARK: - Reason
struct Reason: Codable {
let id: Int
let reasons: String
}
typealias Reasons = [Reason]
Here is your server response in JSON String formate:
let json = """
[
{
"id": 1,
"reasons": "Test"
},
{
"id": 3,
"reasons": "Test"
}
]
"""
Then I have converted it to Data only for testing purpose:
let data = json.data(using: .utf8)!
And you can parse it with
do {
self.reasonsData = try JSONDecoder().decode(Reasons.self, from:data)
for item in self.reasonsData {
print(item.reasons) //This will print reasons.
}
}
catch {
print(error)
}
UPDATE:
If you want all reasons in one string then replace
for item in self.reasonsData {
print(item.reasons)
}
with
let errMsg = self.reasonsData.compactMap { $0.reasons }.joined(separator: "\n")
Essentially I have the following function:
func stagedCount() {
let url = "example.com"
let parameters: Parameters =
["person": "\(name)"]
Alamofire.request(url, method: .post, parameters: parameters).responseData(completionHandler : { response in
if let allObjects = response.result.value as? NSArray{
print("Array length is \(allObjects.count)")
}
}
)}
The JSON response from the URL looks like this:
[
{
"person": "Jake",
"hobby": "soccer"
},
{
"person": "Mary",
"hobby": "surfing"
}
]
I am getting the following response and no count is currently printing.
Cast from 'Data?' to unrelated type 'NSArray' always fails
How can I fix this to get a count of the amount of records in the array? The result I am looking for is 2
The error is pretty clear. responseData will return a JSON String data. You can not convert Data to an Array. What you need is to decode your json data into an array of person structures:
struct Person {
let person: String
let hobby: String
}
when decoding the response:
guard
let data = response.data,
let json = String(data: data, encoding: .utf8)
else { return }
print("json:", json)
do {
let people = try JSONDecoder().decode([Person].self, from: data)
print(people.count)
for person in people {
print(person)
}
} catch {
print(error)
}
I am trying to format my response from a server in an array of multiple object JSON. I tried slice and []Struct but I believe I am not able to understand their usage correctly.
I tried the method mentioned here - https://play.golang.org/p/9OEPzbf0Me0 but didn't work in my case
I am using golang to get response for a query parameter
router.HandleFunc("/v1.0/singles", GetInfo).Methods("GET").Queries("Group", "{group}")
//controller.go
func SendToServer(jsonValue []byte, command string) (models.JSON, error) {
var pdatar models.JSON
s := []string{"http://172.xx.xxx.xx:xxxxx/", command}
response, err := http.Post(strings.Join(s, ""), "application/json", bytes.NewBuffer(jsonValue))
data, err := ioutil.ReadAll(response.Body)
err = json.Unmarshal(data, &pdatar)
mdatar := make(models.JSON)
mdatar["ID"] = pdatar["id"]
mdatar["Name"] = pdatar["name"]
fmt.Println(mdatar) //This is how it is coming map[ID:[877,235], Name:[FCU, GHP]]
/*Another attempt
var temp models.Arr
json.Unmarshal(data, &temp)
fmt.Println(temp) ---- returning null
*/
return mdatar, nil
}
func (c Controller) GetSingle(pdata models.Single) (models.JSON, error) {
mdata := make(models.JSON)
mdata["group"] = pdata.Group
jsonValue, err := json.Marshal(mdata)
pdatar, err := SendToServer(jsonValue, "func_info")
return pdatar, nil
// json.NewEncoder(w).Encode(pdatar) //sending this to my main function
}
//model.go
type JSON map[string]interface{}
type Single struct {
ID int `json:"id"`
Name string `json:"name"`
}
type Data []Single
type Arr [][]string
I have a response from my API in a format
{
"id":[877,235,312,429],
"name" ["FCU","GHP","c","d"],
"group":["x","x","y","z"]
}
With Current Code I am receiving a response
{"id":[877 235], "name": ["FCU" "GHP"]
Though I am expecting a response
{
"data":
[
{"id": 877
"name" : "FCU"
}
{"id": 235
"name": "GHP"
}
]
}
Your mdatarr doesn't change the format any, so this result makes sense.
If the data received from the server is in the format:
{
"id": [877,235,312,429],
"name" ["FCU","GHP","c","d"],
"group": ["x","x","y","z"]
}
then you will need translate it. Ideally you'd fix the server so that it sends data in the format:
{
{
"id": 877,
"name": "FCU",
"group": "x",
},
...
}
If the server could send something like the above, then you could simply unmarshal it into a models.Data object.
If that is not an option, and you really need to do the translation client-side, then you'll need a for loop that does something like this:
ids := pdatarr["id"]
names := pdatarr["name"]
...
if len(ids) != len(names) {
return nil, errors.New("Invalid input format")
}
var mdatar models.Data
for i := range(ids) {
mdatar = append(mdatar, models.Single{ids[i], names[i], ...})
}
return models.JSON{"data": mdatar}, nil
My input json data is this (cannot be changed, from an external resource):
[{
"Url": "test.url",
"Name": "testname"
},{
"FormName": "Test - 2018",
"FormNumber": 43,
"FormSlug": "test-2018"
}]
I have two structs that will always match the data within the array:
type UrlData struct{
"Url" string `json:Url`
"Name" string `json:Name`
}
type FormData struct{
"FormName" string `json:FormName`
"FormNumber" string `json:FormNumber`
"FormSlug" string `json:FormSlug`
}
Obviously the code below will not work, but is it possible at the top level (or otherwise) to declare something like this:
type ParallelData [
urlData UrlData
formData FormData
]
Use a two step process for unmarshaling. First, unmarshal a list of arbitrary JSON, then unmarshal the first and second element of that list into their respective types.
You can implement that logic in a method called UnmarshalJSON, thus implementing the json.Unmarshaler interface. This will give you the compound type you are looking for:
type ParallelData struct {
UrlData UrlData
FormData FormData
}
// UnmarshalJSON implements json.Unmarshaler.
func (p *ParallelData) UnmarshalJSON(b []byte) error {
var records []json.RawMessage
if err := json.Unmarshal(b, &records); err != nil {
return err
}
if len(records) < 2 {
return errors.New("short JSON array")
}
if err := json.Unmarshal(records[0], &p.UrlData); err != nil {
return err
}
if err := json.Unmarshal(records[1], &p.FormData); err != nil {
return err
}
return nil
}
Try it on the playground: https://play.golang.org/p/QMn_rbJj-P-
I think Answer of Peter is awesome.
Option 1:
type ParallelData [
urlData UrlData
formData FormData
]
if you need above structure then you can define it as
type UrlData struct {
Url string `json:"Url,omitempty"`
Name string `json:"Name,omitempty"`
}
type FormData struct {
FormName string `json:"FormName,omitempty"`
FormNumber string `json:"FormNumber,omitempty"`
FormSlug string `json:"FormSlug,omitempty"`
}
type ParallelData struct {
UrlData UrlData `json:"UrlData,omitempty"`
FormData FormData `json:"FormData,omitempty"`
}
In this case, your json will look like
[
{
"UrlData":{
"Url":"test.url",
"Name":"testname"
}
},
{
"FormData":{
"FormName":"Test - 2018",
"FormNumber":"43",
"FormSlug":"test-2018"
}
}
]
Option 2:
You've provide following json:
[
{
"Url":"test.url",
"Name":"testname"
},
{
"FormName":"Test - 2018",
"FormNumber":43,
"FormSlug":"test-2018"
}
]
If your json really look like, then you can use following struct
type UrlData struct {
Url string `json:Url`
Name string `json:Name`
}
type FormData struct {
FormName string `json:FormName`
FormNumber int `json:FormNumber`
FormSlug string `json:FormSlug`
}
type ParallelData struct {
UrlData
FormData
}
For both options, you can Unmarshall your json like this
var parallelData []ParallelData
err := json.Unmarshal([]byte(str), ¶llelData)
if err != nil {
panic(err)
}
fmt.Println(parallelData)
See option 1 in playground
See option 2 in playground
You can unmarshal into a map[string]interface{} for example:
type ParallelData map[string]interface{}
func main() {
textBytes := []byte(`[
{
"Url": "test.url",
"Name": "testname"
},
{
"FormName": "Test - 2018",
"FormNumber": 43,
"FormSlug": "test-2018"
}]`)
var acc []ParallelData
json.Unmarshal(textBytes, &acc)
fmt.Printf("%+v", acc)
}
Output:
=> [map[Url:test.url Name:testname] map[FormName:Test - 2018 FormNumber:43 FormSlug:test-2018]]
Playground
I am trying to get each JSON object out of a JSON array. I get this data via a HTTP post.
I know what my data will look like:
{
"array":[
{
"entity_title":"University of Phoenix",
"entity_org_name":"CS Club",
"possible_user_name":"Johnny Ive",
"posibble_user_email":"Johhny.Ive#uop.edu",
"user_position_title":"President",
"msg_body_id":4
},
{
"entity_title":"University of San Francisco",
"entity_org_name":"Marketing club",
"possible_user_name":"steve jobs",
"posibble_user_email":"steven.job#uop.edu",
"user_position_title":"Student",
"msg_body_id":5
}
]
}
My example code and my structs look like this:
type MsgCreateUserArray struct {
CreateUser []MsgCreateUserJson `json:"createUserArray"`
}
type MsgCreateUserJson struct {
EntityTitleName string `json:"entity_title_name"`
EntityOrgName string `json:"entity_org_name"`
PossibleUserName string `json:"possible_user_name"`
PossibleUserEmail string `json:"possible_user_email"`
UserPositionTitle string `json:"user_position_title"`
MsgBodyId string `json:"msg_body_id, omitempty"`
}
func parseJson(rw http.ResponseWriter, request *http.Request) {
decodeJson := json.NewDecoder(request.Body)
var msg MsgCreateUserArray
err := decodeJson.Decode(&msg)
if err != nil {
panic(err)
}
log.Println(msg.CreateUser)
}
func main() {
http.HandleFunc("/", parseJson)
http.ListenAndServe(":1337", nil)
}
I am not sure where how to iterate over the JSON array and get the JSON objects and then just work with the JSON objects.
Try this as your structs,
type MsgCreateUserArray struct {
CreateUser []MsgCreateUserJson `json:"array"`
}
type MsgCreateUserJson struct {
EntityOrgName string `json:"entity_org_name"`
EntityTitle string `json:"entity_title"`
MsgBodyID int64 `json:"msg_body_id,omitempty"`
PosibbleUserEmail string `json:"posibble_user_email"`
PossibleUserName string `json:"possible_user_name"`
UserPositionTitle string `json:"user_position_title"`
}
Your entity_title_name is not named correctly, nor is the top level array. After you decode into a MsgCreateUserArray you can iterate over the CreateUser slice to get each MsgCreateUserJson