Click here to Skip to main content
16,022,296 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,
I have a simple web api in c# with one control where i just displayed this:
{
  "ClienteID": "A-32222",
  "Mercancia": "Un libro",
   NombreCliente":"Jose perez",
   "Estatus paquete": "Disponible"
},

{
  "ClienteID": "A-32222",
  "Mercancia": "Un libro",
   NombreCliente":"Jose perez",
   "Estatus paquete": "Disponible"
},


But i have query where i have a header like this:
cliente NombreCliente Cantidad Paquete Estatus
A-32222 Jose perez 20 Disponible
A-32222 Jose perez 10 En aduana
etc
My question or questions:
1. How could i join both json to have first the header and then details:
{
    "NombreCliente:"  Jose perez,
    "Cantidad Paquete" 20,
    "Estatus":  "Disponible",
    "Cantidad Paquete": "10",
    "Estatus":  "En aduana"
  }
  [
   //Here all the details
  ]
  }


Any help?
Thanks in advanced.

What I have tried:

I had tried serveral examples, but i do not understand
Posted
Updated 14-Jul-24 11:50am
v2

Unfortunately, you can't create the JSON that you are after because what you are after is not valid JSON. I know that sentence sounds a bit odd, but let me explain. In one part, you have the following:
JSON
{
  "NombreCliente:"  Jose perez,
  "Cantidad Paquete" 20,
  "Estatus":  "Disponible",
  "Cantidad Paquete": "10",
  "Estatus":  "En aduana"
}
You cannot have repeating key/value pairs in the same portion of JSON. Where you have Cantidad Paquete and Estatus, you can only have one of these present. What you need to do here is create an array of attributes to represent these elements. Something like this:
JSON
{
  "NombreCliente:"  Jose perez",
  "Details" [
     "Item": {
       "Cantidad Paquete": 20,
       "Estatus":  "Disponible"
     },
     "Item": {
      "Cantidad Paquete": 10,
      "Estatus":  "En aduana"
     }
  ]
}
How this maps back to your original request is going to depend on what you want to pull out of the original details. Assuming you want it to look like this:
JSON
{
  "NombreCliente:"  Jose perez",
  "ClienteID": "A-32222",
  "Mercancia": "Un libro",
  "Details" [
     "Item": {
       "Cantidad Paquete": 20,
       "Estatus":  "Disponible"
     },
     "Item": {
      "Cantidad Paquete": 10,
      "Estatus":  "En aduana"
     }
  ]
}
If that's what you wanted to display, you would have to map between the two lists based on a key value that is shared (e.g. the ClienteID). Once you have your classes in place, you could use LINQ to help shape the population of the data.
 
Share this answer
 
Comments
Luis M. Rojas 15-Jul-24 8:41am    
Thanks, thanks a lot. That is what i want.
I do not know LINQ, but i am going to see some examples.
Really thanks
Pete O'Hanlon 15-Jul-24 8:44am    
You are most welcome. Good luck.
Start by constructing exactly what you want in the form of C# classes, and use your code to fill the data in.
Then use a JSON package such as NewtonSoft - available via NuGet: NuGet Gallery | Newtonsoft.Json 13.0.3[^] - to generate a JSON string. That's the JSON format you want, and it will be directly readable back into the classes your app is working with.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900