0

I have an array of 300 objects which each object contain many parameters and I want to loop through the array to create a JSON object which contain only two parameters. my objects were created from this json structure in the array :

 "publicationId": 13,
            "name": null,
            "code": "FIGA_20190516",
            "parutionDate": "2019-05-16",
            "quadri": "FIGA",
            "ratio": null,
            "numero": "0",
            "createdAt": "2019-06-18 21:29:21",
            "nbPages": 0,
            "search": 1,
            "down": 1,
            "thumb_height": 0,
            "quartPageWidth": 0,
            "quartPageHeight": 0,
            "pdfHeight": "0",
            "pdfWidth": "0",
            "PDF": "2019-06-18 21:29:21",
            "XML": null,
            "SendKM": "2019-06-18 21:29:21",
            "KM": null,
            "Melody": null,
            "filename": null,
            "SendNormalize": null,
            "Normalized": null,
            "librelioSubTitle": null,
            "SendProcessArticle": null,
            "ProcessedArticles": null,
            "melodyCallBackUrl": null,
            "htmlMilibris": false,
            "htmlHarry": false,
            "sendProcessLibrelio": null,
            "processedLibrelio": null,
            "id": 77066,
            "_solrDocument": {},
            "_repository": "FIGA/2019/05/FIGA_20190516/"

and I want to loop through the array to create a JSON object with this struct :

"availableParutions" : {
        id : createdAt,
        id : createdAt,
        id : createdAt
    }

I really appreciate your help guys !

6
  • 1
    Hi @mark, where did you get JSON data? Service or is it involved in the project? Commented Jun 27, 2019 at 14:03
  • Have you checked Codable? Commented Jun 27, 2019 at 14:07
  • Is it the same as: stackoverflow.com/questions/42081141/…? Commented Jun 27, 2019 at 14:08
  • Hi @Emre it's a service Commented Jun 27, 2019 at 14:10
  • hey @Ahmed nope it's not the same structure I can create a structure of array of objects [{obj},{obj},{obj}] like the exemple you posted but this is an other format {key:value, key:value, key:value} Commented Jun 27, 2019 at 14:12

3 Answers 3

1

You can create a new struct that represents the data you want in your Json output

struct NewStruct: Encodable {
    let id: Int
    let createAt: String
}

This struct conforms to Encodable so that it can be encoded to json but since it is so simple no extra coding is needed

Then you can convert your array of large objects to an array of NewStruct using map

let newArray = array.map { NewStruct(id: $0.id, createAt: $0.createdAt) }

If I understand it correctly this array should be a property named "availableParutions" so lets make an outer struct for the json

struct Output: Codable {
    let availableParutions: [NewStruct]
}

and load it

let output = Output(availableParutions: newArray)

and encode it

do {
    let jsonData = try JSONEncoder().encode(output)
} catch {
    print(error)
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Joakim you saved me !
1

You can try the following code:

Note: I have create json obj my self for evaluation purpose, you can use your json directly.

    let json = [
        [
        "publicationId":11,
        "createdAt":"2019-06-18 21:29:21",
        "id":77063,
        ],
        [
            "publicationId":12,
            "createdAt":"2019-06-18 21:29:21",
            "id":77065,
        ],
        [
            "publicationId":13,
            "createdAt":"2019-06-18 21:29:21",
            "id":77066,
        ]
        ]

 var jsonArr: [[String:Any]] = [[String:Any]]()
        for obj in json {
            let id = obj["id"] as! Int
            let jsonObj = [
                "\(id)": obj["createdAt"] as! String
            ]
            jsonArr.append(jsonObj as [String : Any])
        }
        print(jsonArr)

1 Comment

Hey @Bhavik ! thank you for your reply, so first my array dosen't contain json it contain objects created from the passed json format, second I tested your code and it gives me this format:[["77063": "2019-06-18 21:29:21"], ["77065": "2019-06-18 21:29:21"], ["77066": "2019-06-18 21:29:21"]] and it don't match the format I want, thank you for your try
0

You could create an empty dictionary, then iterate over json array and pull out id & createdAt. Update dictionary with id & createdAt. Finally set this dictionary as value of availableParutions in a new dictionary.

code:

    var map: [Int: Any]  = [:]
    for item in json {
        let id = item["id"] as! Int
        map[id] = item["createdAt"]!
    }
    let outerObject = ["availableParutions": map]
    print(outerObject)

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.